GM Version: Studio 2
Target Platform: Windows / All
Download: n/a
Links: n/a
[PROBLEM]
When you create a reference of an array and edit it your original array stays the same and only the "copy" gets modified.
[EXPLANATION]
There are four different array behaviour you need to understand.
(1) Editing an array directly will edit the original array and consequently ALL the references.
(2) Assigning an array to a new variable will just share the pointer to that array
(3) Editing a reference to an array will only edit the reference and not the original
(4) This is similar to the third one, passing an array as an argument counts as making a reference to it.
This last one happens because the "array" variable of the function is actually a reference to the array1, so in the backstage GMS does
It was like this all the way back from GMS1.4 if I'm not mistaken, so this is NOT new to 2.3.
[ACCESSORS]
You probably heard of accessors before specially if you used ds_lists, ds_maps, ds_grids. Those are symbols that allow for quick edit of entries in the respective data structures
[SOLUTION]
What many people don't know is there is a special accessor for arrays too, that apart from allowing quick access to values stops GMS from copying the original array and instead makes an in-place edit.
The array accessor is "@" (at symbol)
So whenever you want to edit the original array instead of copying it you just need to use:
[NOTE]
The array accessor allows for an edit-in-place so as you might have guessed you don't need to use it when reading from an array;
Here xD from xDGameStudios,
Good coding to you all.
Target Platform: Windows / All
Download: n/a
Links: n/a
[PROBLEM]
When you create a reference of an array and edit it your original array stays the same and only the "copy" gets modified.
[EXPLANATION]
There are four different array behaviour you need to understand.
(1) Editing an array directly will edit the original array and consequently ALL the references.
GML:
var array1 = [1, 2, 3, 4, 5];
var array2 = array1;
array1[0] = "Hello";
show_debug_message(array1) // ["Hello", 2, 3, 4, 5];
show_debug_message(array2) // ["Hello", 2, 3, 4, 5];
GML:
var array1 = [1, 2, 3, 4, 5];
var array2 = array1;
show_debug_message(array1) // [1, 2, 3, 4, 5]; <- same pointer
show_debug_message(array2) // [1, 2, 3, 4, 5]; <- same pointer
GML:
var array1 = [1, 2, 3, 4, 5];
var array2 = array1;
array2[0] = "Hello";
show_debug_message(array1) // [1, 2, 3, 4, 5];
show_debug_message(array2) // ["Hello", 2, 3, 4, 5];
GML:
function myFunc(array) {
array[0] = "Hello";
}
var array1 = [1, 2, 3, 4, 5];
myFunc(array1);
show_debug_message(array1) // [1, 2, 3, 4, 5];
array = array1
and then passes the argument to the function. Every time you set a variable to an array that variable is given the pointer to the array, BUT as soon as you edit it.. the array gets copied and dereferenced from the original.It was like this all the way back from GMS1.4 if I'm not mistaken, so this is NOT new to 2.3.
[ACCESSORS]
You probably heard of accessors before specially if you used ds_lists, ds_maps, ds_grids. Those are symbols that allow for quick edit of entries in the respective data structures
GML:
myList[| 0] = 12; // the accessor is -> |
myMap[? "name"] = "value"; // the accessor is -> ?
myGrid[# 1, 2] = true // the accessor is -> #
[SOLUTION]
What many people don't know is there is a special accessor for arrays too, that apart from allowing quick access to values stops GMS from copying the original array and instead makes an in-place edit.
The array accessor is "@" (at symbol)
So whenever you want to edit the original array instead of copying it you just need to use:
GML:
var array1 = [1, 2, 3, 4, 5];
var array2 = array1;
array2[@ 0] = "Hello";
show_debug_message(array1) // ["Hello", 2, 3, 4, 5]; <- same pointer
show_debug_message(array2) // ["Hello", 2, 3, 4, 5]; <- same pointer
[NOTE]
The array accessor allows for an edit-in-place so as you might have guessed you don't need to use it when reading from an array;
GML:
var array1 = [1, 2, 3, 4, 5];
var value = array1[@ 0] // this is not needed and as far as I know carries some overhead.
Here xD from xDGameStudios,
Good coding to you all.
Last edited: