Reputation: 11
I am new to Game maker studio and GML and I am trying to fill a border with a grid and squares. The color of the square will depend on an indexed value in a global array. I keep getting an error "Variable Index [8] out of range [0]"
the create event for the border is:
global.map= [0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0]
for (ii=0; ii<8; ii++) {
for (jj=0; jj<8; jj++) {
// Create the grid
instance_create_layer(x + (ii*sprite_get_width(Spr_Hoop)),
y + (jj*sprite_get_height(Spr_Hoop)),
"Instances",
Obj_Hoop)
//Create the square
instance_create_layer(x+1 + (ii*(sprite_get_width(Spr_Square)+2)),
y+1 + (jj*(sprite_get_height(Spr_Square)+2)),
"Instances",
Obj_Square)
}
}
And the draw event for the squares is:
if(global.map[Obj_Border.ii][Obj_Border.jj]) {
//if(global.map[0][0]) {
image_blend = c_white
}
else {
image_blend=c_black
}
draw_self()
I assume it is a simple fix thank you Andrew
Upvotes: 0
Views: 337
Reputation: 588
Like Steven said your global.map
is a single array but you're accessing it as if it was a two dimensional array. Making some minor adjustments to your original code you can have it draw a grid like so:
// Init Global Array
global.map[0] = [0,0,0,1,1,0,0,0];
global.map[1] = [0,0,0,1,1,0,0,0];
global.map[2] = [0,0,0,1,1,0,0,0];
global.map[3] = [1,1,1,1,1,1,1,1];
global.map[4] = [1,1,1,1,1,1,1,1];
global.map[5] = [0,0,0,1,1,0,0,0];
global.map[6] = [0,0,0,1,1,0,0,0];
global.map[7] = [0,0,0,1,1,0,0,0];
// Draw global.map as a grid
for (ii=1; ii<9; ii++) {
for (jj=1; jj<9; jj++) {
draw_rectangle(15,30,15 + ii * 32, 30 + jj * 32, true);
draw_text(20 + (ii-1) * 32,35 + (jj-1) * 32 , global.map[ii-1, jj-1]);
}
}
Then you would evaluate if grid.map[ii, jj]
is equal to 0, 1, or whatever value you want and create branches from there. Notably for debugging you can also use this to see if the values displaying correctly draw_text(x + 15 + ii * 32, y + 300 + jj * 32 , global.map[ii, jj]);
(I did test this in https://yal.cc/r/gml/ first so this should do the trick!).
Hopefully this helps.
Upvotes: 2
Reputation: 2122
Your global.map
is just a single array, but your code:
if(global.map[Obj_Border.ii][Obj_Border.jj])
is addressing the array as if it's a multidimensional array.
So try converting the global.map
into a multidimensional array (or 2D array).
Or alternatively, change the code that calls the global.map
that it only reads out of a single array.
I've not practised with 2D arrays in game-maker, so unfortunately, I cannot give an example.
Upvotes: 1