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

Windows Need help

G

Giang Gia Hùng

Guest
hello guys, i need help with this
GML:
//spawn_enemy script:


spawn_points();
for(var i = 0; i <= 2; i++)
{
    for(var a = 0; i <= 2; a++)
    {
        instance_create_layer(_x[a],_y[i],"Instances",o_Grammar_Nazi);
        a++;
    }
    i++;
}


//spaw_point script:


// X
_x[0] = 32;
_x[1] = 128;
_x[2] = 224;
// Y
_y[0] = 128;
_y[1] = 224;
_y[2] = 320;


//object collision event with object o_Spawn_Enemy:


instance_destroy(o_Spawn_Enemy);
motion_set(0,0);
spawn_enemy();
when i run the code i get this error:

GML:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Evento_Spawn_Enemy
for object o_Spaceship:

Push :: Execution Error - Variable Index [0,4] out of range [1,3] - -1._x(100011,4)
 at gml_Script_spawn_enemy (line 6) -               instance_create_layer(_x[a],_y[i],"Instances",o_Enemy);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_spawn_enemy (line 6)
called from - gml_Object_o_Spaceship_Collision_dabb57ab_ce59_46cc_8462_11c9618e68ae (line 3) - spawn_enemy();
why does the game keep exceeding the array variable range index? can you guys explain to me why? Because i'm quite new to GMS 2 so i dont know if it is because i put it in the wrong event.
 

johnwo

Member
Continuing off of what @EvanSki said:

GML:
spawn_points();
for(var i = 0; i <= 2; i++)
{
    for(var a = 0; i <= 2; a++)
    {
        instance_create_layer(_x[a],_y[i],"Instances",o_Grammar_Nazi);
        a++; // <-- REMOVE THIS
    }
    i++; // <-- REMOVE THIS
}
A FOR-LOOP consist of 3 things:
  • Initialization
  • Condition
  • Afterthought
Initialization executes once, before the loop starts.
The condition is checked before the loop is executed; if it is true, then the loop is executed.
The afterthought is performed after an iteration of the loop.

You already have i++ and a++ in the afterthought of the loop, so there is no need to have it in the loop itself.
 
Top