• 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!

variable referencing an array ?

DaveInDev

Member
I'm a bit lost. I thought that assigning an array to a variable was not copying it, but just making a reference to this array ?
Why is this code not working as I thought it should ?
Looking at the debugger, it seems that t is another array of its own, distinct from global.arr ...

GML:
global.arr= [
    [1,1,1],
    [2,2,2]
]

function test()
{
    var t;
    
    t = global.arr[0];
    
    t[1] = 10;    // does not modify global.arr ?
    t[2] = 12;
}
 
GM's arrays are 'copy-on-write' for any new references to an existing array. You can use the @ accessor to modify the original array without copying:
GML:
t[@ 0] = 10;
 

DaveInDev

Member
Oh ok, I thought that this @ accessor was only used when you were passing an array as a parameter in a function, but infact it should be used as soon as you try to reference an array and want to modify this reference ! Thanks.
 

DaveInDev

Member
Just a complementary question to be sure...
When working on a reference to an array, are the function like array_insert, array_delete, etc... working correctly on the original referenced array ? Only the [] need an accessor to disambiguate , right ?
 
Top