Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Best way of figuring out the length of an array that is multidimensional ????

Author
Message
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 20th Nov 2018 12:33
Heey everyone!

To all you coding gurus out there; is there a way of quickly figuring out an arrays length? I'm trying to implement a level saving / loading functionality and am wondering if I need to use a separate array to just store the lengths of multidimensional arrays and restore the level from there. I am intending to use the built-in functionality of saving arrays and JSONs. Can I read the lengths from those somehow?

In terms of code, I'm setting a random map size for the X axis and Y axis, then use a MAP[size_x,size_y] array to fill it up with objects. I can restore the placed tiles just by reading their x,y,z locations which I save also, however the MAP[a,b] array is used for pathfinding / player seeking and some behaviours, so it shouldn't be empty. Same goes for arrays that save enemy positions for example.

Anyway, some tips are greatly appreciated

The author of this post has marked a post as an answer.

Go to answer

puzzler2018
User Banned
Posted: 20th Nov 2018 12:59 Edited at: 20th Nov 2018 13:00
Yes - an array length can be achieved by putting .Length on the end



Im not sure about multidimension arrays though not without testing but I think It works too in the same way

Others here will be able to correct you on this if that's not the case
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 20th Nov 2018 13:45
Yeah, the .length seems to return only the length of the first dimension, or at least I wouldn't know the correct syntax for the other dimension otherwise.
In your example, how would you deduce / extract the Size_Y without manually storing that value yourself?


Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 20th Nov 2018 13:48 Edited at: 20th Nov 2018 13:52
This post has been marked by the post author as the answer.
try:
arr.length = x
arr[0].length = y
arr[0,0].length = z
....

[/url]
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 20th Nov 2018 13:52 Edited at: 20th Nov 2018 13:53
edit: Ahhhh nevermind my comment below, thank you Jack that's exactly what I'm looking for!!

I think I have just found it :


Not sure if the [0] can be set to any higher dimension, it will return whatever length the second dimension in the array is even with say map[30].length
It doesn't seem to be in the manual though!!!
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 20th Nov 2018 14:01 Edited at: 20th Nov 2018 14:17
Quote: "
try:
arr.length = x
arr[0].length = y
arr[0,0].length = z
"


So I guess we can also do something like this then??

arr.find(x)
arr[0].find(y)
arr[0,0].find(z)

edit: fixed syntax

That actually would solve some other things as well.
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 20th Nov 2018 14:21
Hmmmm, it seems the .find works differently.

I can search with arr[0].find(x) or if I want to search the next dimension with arr[1].find(y)
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 20th Nov 2018 17:41
.find() only works for sorted arrays. For unsorted, you will need to use a for loop and check each item of the array.

Login to post a reply

Server time is: 2024-04-19 03:55:32
Your offset time is: 2024-04-19 03:55:32