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

GameMaker ERROR - Variable Index [3] out of range [3] (SOLVED)

Hi ya'll!

So I'm getting this error, I was following a tutorial on youtube, anyways I was doing good, and then afterward this popped up and I've looked through all I know and rewatched the video multiple times. I'm still pretty new to all this so I'm stumped.

Here's the error I got:

############################################################################################
ERROR in
action number 1
of Draw Event
for object obj_title_menu:

Variable Index [3] out of range [3]
at gml_Object_obj_title_menu_Draw_0 (line 5) - var _op_w = string_width(option[menu_level, i]);
############################################################################################
gml_Object_obj_title_menu_Draw_0 (line 5)


And here is the code I have:

//dynamically get width and height of menu
var _new_w =0;
for (var i = 0; 1 < op_length; i++)
{
var _op_w = string_width(option[menu_level, i]);
_new_w = max(_new_w, _op_w);
}
width = _new_w + op_border*2;
height = op_border*2 + string_height(option[0, 0]) + (op_length-1)*op_space;

//center menu
x = camera_get_view_x(view_camera[0]) + camera_get_view_width(view_camera[0])/2 - width/2;
y = camera_get_view_y(view_camera[0]) + camera_get_view_height(view_camera[0])/2 - height/2;

//draw menu background
draw_sprite_ext(sprite_index, image_index, x, y, width/sprite_width, height/sprite_height, 0, c_white, 1);

//draw options
draw_set_font(global.font_main);
draw_set_valign(fa_top);
draw_set_halign(fa_left);
for (var i = 0; 1 < op_length; i++)
{
var _c = c_white;
if pos == i {_c = c_yellow};
draw_text_color(x+op_border, y+op_border + op_space*i, option[menu_level, i], _c, _c, _c, _c, 1);
}


Thanks ya'll!
 

gnysek

Member
That means you want to access element [3] in array which size is [3] (so indexes are 0, 1, 2 - 3 elements in total). That error message could be formatted better, that's true. Place with wrong code is clearly described by @FrostyCat , but I can add from myself, that if 1, i, l are too similar in font you're using, you may also try different one in code editor - I'm personally using JetBrains Mono font (it's free to use).
 
Do you know the difference between 1 < op_length and i < op_length?

Turn up the resolution on the video so that you can see every character clearly.
*cough* You know, I totally knew that... I was definitely just testing your attention to detail.

Thank you so much, It's always the little things for me that I miss.
 
That means you want to access element [3] in array which size is [3] (so indexes are 0, 1, 2 - 3 elements in total). That error message could be formatted better, that's true. Place with wrong code is clearly described by @FrostyCat , but I can add from myself, that if 1, i, l are too similar in font you're using, you may also try different one in code editor - I'm personally using JetBrains Mono font (it's free to use).
Thanks a bunch, I think I will use that, I will definitely help for the future!
 
Top