Hi GMC!
I have a question about arrays that I'm not getting and I think it's best explained with a demonstration.
Consider the following code in the Create Event of a newly created object called
This...results in a fatal error:
However, interestingly, if I use the
The game runs like normal and the output log tells me that
Even more interestingly though, if I change line
The game will STILL run and also give me the same output.
The manual explains the use of the
IDE Version: 2.3.1.542
Current Runtime: 2.3.1.409
Any replies appreciated!
I have a question about arrays that I'm not getting and I think it's best explained with a demonstration.
Consider the following code in the Create Event of a newly created object called
objArrayTester
:
GML:
//Example #1
function addOne(array) {
array[0] += 1;
}
a = array_create(1, 0);
addOne(a);
show_debug_message(a);
However, interestingly, if I use the
@
accessor in the function like so:
GML:
//Example #2
function addOne(array) {
array[@ 0] += 1;
}
a = array_create(1, 0);
addOne(a);
show_debug_message(a);
a
contains [ 1 ]
:Even more interestingly though, if I change line
array[@ 0] += 1;
to array[0] = array[0]+1;
, so that it's:
GML:
//Example #3
function addOne(array) {
array[0] = array[0]+1;
}
a = array_create(1, 0);
addOne(a);
show_debug_message(a);
a
contains [ 1 ]
. I'm not returning the array at all, so this shouldn't be the case, right?The manual explains the use of the
@
accessor used to directly reference/modify the contents of an array instead of making a copy. Handy. It also explains that without it, a copy of the array is created, modifying that instead, not the original thing. So, I don't understand why Example #1 crashes without the use of the @
accessor or why the contents of a
change in Example #3.IDE Version: 2.3.1.542
Current Runtime: 2.3.1.409
Any replies appreciated!