• 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] Quick question regarding how arrays are passed

W

Wayfarer

Guest
I have a script which refers to the array "global.grid" quite a bit, so I've made it a local variable like so:
Code:
var grid = global.grid;
Now, I notice if I do the following...
Code:
var grid = global.grid;
global.grid[0, 0] = "!!!";
show_message(grid);
...that correctly show the "!!!" in the first cell.

However, if I do...
Code:
var grid = global.grid;
global.grid = 0;
show_message(grid);
...it still shows the original array. Is the array still existing because it's referenced by the local "grid" variable? Shouldn't setting it to 0 free the array?

I'm making this post because I want to ensure by creating a local variable I'm not constantly making a copy of the array each step.
 

Paskaler

Member
The arrays in GameMaker are passes in as a reference, until you make a chanhe to them, like you did in the second code snippet. If you moved that show_message out of the script, it wouldn't show "!!!" in the 0,0 location. If you want to modify the original array, you have to use an accessor:

Code:
global.grid[@ 0, 0] = "!!!";
If you're just accesing values from an array, and not modifying, then you don't have to use an accessor, that is, the array gets copied on first write.
 
W

Wayfarer

Guest
I think I get what you're saying. So when "global.grid" is set to "0" the local "grid" variable becomes its own copy of the original array before it was modified?

Also, just for clarity, in your accessor example, wouldn't it be:
Code:
grid[@ 0, 0] = "!!!";
without the "global."? Because if you were using the "global." you wouldn't need the accessor? Just want to make sure I'm understanding this right.

I feel like I'm getting deja vu now, maybe I made a post asking these same questions before :p

Though I still feel a bit confused:
The arrays in GameMaker are passes in as a reference, until you make a chanhe to them, like you did in the second code snippet.
The part that confuses me is why does "grid" correctly reflect the change made to "global.grid"? If I changed it shouldn't it now be a copy with the original values?


Edit:
Like if you look at this example here:
Code:
var grid = global.grid;

global.grid[0, 1] = "some change 1";
global.grid[0, 2] = "some change 2";
global.grid[0, 3] = "some change 3";

show_debug_message(grid[0, 1]); // outputs "some change 1"
show_debug_message(grid[0, 2]); // outputs "some change 2"
show_debug_message(grid[0, 3]); // outputs "some change 3"
Even though I've changed the original array, the local variable still seems to reference the array correctly. If it was a copy wouldn't it be outputting the wrong information here?

Edit 2:
Ah, I realise what's happening now with the above example. Since I'm changing the original variable it updates the array correctly. But if I were to change the local variable that's when I'd end with a copy, and in that situation you could use accessors (as mentioned) to avoid making a copy.
 
Last edited:

FrostyCat

Redemption Seeker
For a set-to-scalar to free an array, there must be no remaining references to the array. Here you have two references, one in the global variable and another in the local variable. Getting rid of the one in the global variable still leaves one more, that's why you can still get at it.
 
Top