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

Array not set before reading (Bug?)

pawbap

Member
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 objArrayTester:
GML:
//Example #1
function addOne(array) {
    array[0] += 1;
}

a = array_create(1, 0);
addOne(a);
show_debug_message(a);
This...results in a fatal error:
Runner_2021-02-16_14-04-12.png

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);
The game runs like normal and the output log tells me that a contains [ 1 ]:
GameMakerStudio_2021-02-16_14-07-06.png

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);
The game will STILL run and also give me the same output. 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!
 

gnysek

Member
Can you add show_debug_message(array) in addOne function to check, what is actually passed into function? You can also use debugger to check.
 

pawbap

Member
Can you add show_debug_message(array) in addOne function to check, what is actually passed into function? You can also use debugger to check.
I tried a couple more places.
GML:
//Example #3
function addOne(array) {
    show_debug_message("A "+string(array));
    array[0] = array[0]+1;
    show_debug_message("B "+string(array));
}

a = array_create(1, 0);
show_debug_message("C "+string(a));
addOne(a);
show_debug_message("D "+string(a));
1613483693727.png

EDIT: The output is this if the attachment doesn't work for whatever reason.
Code:
C [ 0 ]
A [ 0 ]
B [ 1 ]
D [ 0 ]
 
Last edited:

pawbap

Member
I tried a couple more places.
GML:
//Example #3
function addOne(array) {
    show_debug_message("A "+string(array));
    array[0] = array[0]+1;
    show_debug_message("B "+string(array));
}

a = array_create(1, 0);
show_debug_message("C "+string(a));
addOne(a);
show_debug_message("D "+string(a));
View attachment 38019

EDIT: The output is this if the attachment doesn't work for whatever reason.
Code:
C [ 0 ]
A [ 0 ]
B [ 1 ]
D [ 0 ]
Alright, so after that experiment, I went ahead and commented out show_debug_message("A "+string(array));
Output is:
Code:
C [ 0 ]
B [ 1 ]
D [ 1 ]
1613484264970.png
Really...interesting!!
 
Top