• 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] problem with accessing array literals

Is there a documentation on how to use array literals? Unfortunately, I couldn't find anything.

I'm creating a nested array like this:

a = [[1,2],[3,4]];

but have problems with reading and accessing it. What would usually be the expected syntax for working with them?

Thanks for your time!!
 
Last edited:
By doing that, you've created a nested array, so you have to extract the inner array before you can read it, ie:

Code:
b = a[0];
show_message(b[0]); // pops up "1"
Edit: It is also worth noting that "accessor chaining" is on the list of features to be added in the future, so doing things like the above won't be needed anymore.
 
Last edited:

Binsk

Member
To get the value 2 out you'd do something like this:
Code:
var b = a[0];
value = b[1];
value would then contain 2.
 
Thank you so much! And to change a value I would have to go about it the same way?
Something like:
var b = a [0];
b [1] = new value;
 
Not quite. Arrays in GM Studio work as copy-on-write, meaning that as soon as you change an array that has been stored in another variable, a copy is generated. So in you above example, "b" would contain a new copy of the array with the new value in index 1, but the version in "a" would still have the original value. There's two ways to handle this. You can either reassign "b" back into index 0 of "a", or you can use the array accessor character to modify the original value directly, ie:

Code:
b[@ 1] = new_value;
 

jo-thijs

Member
Thank you so much! And to change a value I would have to go about it the same way?
Something like:
var b = a [0];
b [1] = new value;
Almost.
You'll need an accessor to tell GameMaker that you want to change the array being referenced and not create a copy.
It'd look like this:
Code:
var b = a [0];
b [@1] = new value;
That's how I think it goes anyway, but I should double check it...
brb!

EDIT: nvm, I've been ninja'd anyway.

EDIT: I still double checked it and I (as well as stainedofmind) was correct.
 
Last edited:
Top