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

Problem with constructors, arrays and globals

So, I've got this code:
GML:
function ArrayConstructor() constructor {
    my_array[0] = {
        a: 0
    }
    my_array[1] = {
        a: 1
    }
}

global.position = 0;
global.check = 0;

global.constructed_array = new ArrayConstructor();

if (global.check > global.constructed_array.my_array[global.position].a) { // This check is failing, specifically the my_array[global.position] part
    var a = 0;
}
It fails when running in YYC, simply saying:
ERROR in
action number 1
of Create Event
for object Object1:

trying to index variable that is not an array############################################################################################
gml_Object_Object1_Create_0 (line 13)
However, if I change it to this:
GML:
function ArrayConstructor() constructor {
    my_array[0] = {
        a: 0
    }
    my_array[1] = {
        a: 1
    }
}

global.position = 0;
global.check = 0;

global.constructed_array = new ArrayConstructor();

if (global.check > global.constructed_array.my_array[0].a) { // Not referencing the global.position here, using a hard coded value instead
    var a = 0;
}
It works fine.

ALSO, if I change it to this:
GML:
function ArrayConstructor() constructor {
    my_array[0] = {
        a: 0
    }
    my_array[1] = {
        a: 1
    }
}

global.position = 0;
global.check = 0;

global.constructed_array = new ArrayConstructor();

var _pos = global.position;
if (global.check > global.constructed_array.my_array[_pos].a) { // Using a local variable that has taken the value of global.position, instead of global.position itself
    var a = 0;
}
It runs as well.

Can anyone explain to me why this would be the case?
 
Top