• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [Solved] Having trouble with the multi-dimensional array system

A

Amara Angel

Guest
I'm using GMS2 (desktop, windows 2.2.4.464, runtime 2.2.4.372) and I was working on updating some old code after a long time of not using GMS2. I was working with 2D array's, but after a bunch of errors I took a look at the manual and there I saw that the whole system had changed and that stuff like
Code:
array_length_2d
are deprecated. Instead of a[0, 0] you're supposed to do a[0][0] now?

I tried to use my 2D arrays like that instead but doing something like:
Code:
x = RLocation[RLN][0];
returns the compile error
Code:
 Assignment operator expected
and
Code:
 malformed assignment statement
The same things happen when I try to do:
Code:
RLocation[0][0] = x;
Is this supposed to happen? If it is, how am I supposed to access 2D arrays instead?

I have checked and I should be running the latest version of the editor and of the runtime.
 

Simon Gust

Member
Pretty sure 2D arrays still work. The one you are trying here looks somewhat like an array inside of an array which is new in GMS2.
https://help.yoyogames.com/hc/en-us/articles/231539867-GameMaker-Studio-2-New-Functions-List

I think because of this
Code:
a[0, 0]
creates a 1D array with 2 indexes due to that new syntax.
So you would have to do it this way
Code:
for (var i = w-1; i >= 0; i--) {
for (var j = h-1; j >= 0; j--) {
   a[i, j] = 0;
}}
As long as w and h (width and height) are greater than 1 (else why would you create a 2D array) should work.
 
Last edited:
A

Amara Angel

Guest
Pretty sure 2D arrays still work. The one you are trying here looks somewhat like an array inside of an array which is new in GMS2.
https://help.yoyogames.com/hc/en-us/articles/231539867-GameMaker-Studio-2-New-Functions-List

I think because of this
Code:
a[0, 0]
creates a 1D array with 2 indexes due to that new syntax.
So you would have to do it this way
Code:
for (var i = w-1; i >= 0; i--) {
for (var j = h-1; j >= 0; j--) {
   a[i, j] = 0;
}}
As long as w and h (width and height) are greater than 1 (else why would you create a 2D array) should work.
Oh, alright then. Guess I'll go back to that then.
 

CloseRange

Member
I'm a little confused. I just wanted to check so I looked at the wiki.
array_length_2d is still listed, the function shows up perfectly and it says nothing about being depricated.

As well in the game maker 2 wiki it still shows that you should use arrays like array[0, 0] and array[1, 0] for 2 dimensional arrays.
I saw nowhere about using arrays like array[0][1]

Yes in most programming languages you call an array with array[1][2][3] but not in game maker.
If the manual says otherwise please show me where because I could not find any evidence of what you said and I'm curious.

EDIT: guess i'm using older version but as of now arrays are still the same

note.
a[0, 0]
creates an array with 1 index.
the index is located at 0,0 in the array.


you could just initialize an array like this:
Code:
a[w, h] = 0
game maker is smart enough to initialize all the previous values to 0 by default (i think 0 is the default but they are initialized none-the-less)


EDIT: my bad you do have to use a for loop.
 
Last edited:
A

Amara Angel

Guest
I'm a little confused. I just wanted to check so I looked at the wiki.
array_length_2d is still listed, the function shows up perfectly and it says nothing about being depricated.

As well in the game maker 2 wiki it still shows that you should use arrays like array[0, 0] and array[1, 0] for 2 dimensional arrays.
I saw nowhere about using arrays like array[0][1]

Yes in most programming languages you call an array with array[1][2][3] but not in game maker.
If the manual says otherwise please show me where because I could not find any evidence of what you said and I'm curious.

note.
a[0, 0]
creates an array with 1 index.
the index is located at 0,0 in the array.

you could just initialize an array like this:
Code:
a[w, h] = 0
game maker is smart enough to initialize all the previous values to 0 by default (i think 0 is the default but they are initialized none-the-less)
That's only in the online version of the manual. Open it up in the IDE and you'll see the entire section has changed. I usually use the online version of the manual so that was probably how I didn't notice it for so long.
 

CloseRange

Member
except I did use the one in the IDE.
opened up the manuel and searched arrays and found these examples:
array[0, 0] = 1;
array[0, 1] = 2;
array[1, 0] = "one";
array[1, 1] = "two";
array[1, 2] = "three";
array[1, 3] = "four";
array[2, 0] = "1";
array[2, 1] = "2";
array[2, 2] = "3";
then typed in array_length_2d in the code editor, middle mouse clicked it so it opened in the info on it and it didn't say deprecated and I'm running game maker 2 as well
 
A

Amara Angel

Guest
except I did use the one in the IDE.
opened up the manuel and searched arrays and found these examples:

then types in array_length_2d in the code editor, middle mouse clicked it so it opened in the info on it and it didn't say deprecated and I'm running game maker 2 as well
Huh, weird. In the Arrays page in the IDE manual the section talking about 2D arrays (Scripting-> GML Overview-> Arrays) has been completely replaced by "Arrays With More Than One Dimension".
 

Yal

🐧 *penguin noises*
GMC Elder
you could just initialize an array like this:
Code:
a[w, h] = 0
game maker is smart enough to initialize all the previous values to 0 by default (i think 0 is the default but they are initialized none-the-less)
Last time I checked (I'm still using GMS1.4, but the shift from 2D arrays to arrays of arrays should have the same behavior, since independent arrays have even less reason to be rectangular than a single contiguous array) that would create an L-shaped array. a[0] ... a[w-1] would all have 1 element, a[w] would have h elements. You need to set a[0,h] ... a[w-1,h] as well if you want every sub-array initialized to the correct length.
 
A

Amara Angel

Guest
Okay, so that isn't supposed to be there. I'll report this to yoyogames, then.
 

CloseRange

Member
Last time I checked (I'm still using GMS1.4, but the shift from 2D arrays to arrays of arrays should have the same behavior, since independent arrays have even less reason to be rectangular than a single contiguous array) that would create an L-shaped array. a[0] ... a[w-1] would all have 1 element, a[w] would have h elements. You need to set a[0,h] ... a[w-1,h] as well if you want every sub-array initialized to the correct length.
Oh I just tested it and I guess you're right. Guess game make isn't smart enough to do that.
I forgot different rows of a 2d array can have differant sizes. I'm used to arrays having to be rectangular so I got a bit confused for a second.
 

Yal

🐧 *penguin noises*
GMC Elder
Oh I just tested it and I guess you're right. Guess game make isn't smart enough to do that.
I mean, strictly speaking you're not telling it that you want a square array, only that you want an array of size w whose w:th member is an array with h members. The good old "I do what you tell me, not what you want" adage of programming.
 
A

Amara Angel

Guest
Okay, I got a response back. This was indeed unintended and will be fixed in the next IDE update. We still have to use the [x, x] system.
 

Yal

🐧 *penguin noises*
GMC Elder
I have a question, what would the equivalent be of array_height_2d be in the new update?
Take the length of the 2nd array (e.g. if you have an array foo[aa][bb], the height is the length of foo[aa] - note that each of these arrays foo[0], foo[1] etc don't necessarily need to be the same size!)
 
Take the length of the 2nd array (e.g. if you have an array foo[aa][bb], the height is the length of foo[aa] - note that each of these arrays foo[0], foo[1] etc don't necessarily need to be the same size!)
Okay that makes more sense, however let's say I have an array like this foo[0,0] = 1 foo[1,0] = 2

How do I convert this to foo[0][0]?

I hope I'm making sense!
 
Top