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

GML Array says variable index out of range when its not

Carloskhard

Member
So I have an array of 2 dimensions. I have declared the position [0,1] (also the position [0,0] for security, idk). If I access it after creating it ,it works, but if I access it after passing room it gives me an error: Variable index[0,1] out of range [1,1] - - 1.array[100184,1]

*Note that the object containing it its persistent.

BUT if I put that on a debug message likis this:
show_debug_message(icon[0,1]);
in the output it writes the correct number which is saved there!!
How is it telling me that the variable index is out of range if OBVIOUSLY is accesing it and returning the info to the outout at the same time????

This happens only if I put other room before so that makes it even more confusing.
Is there something about 2D arrays Im missing?
What is that error message exactly saying?
Please help.
 
Last edited:

TailBit

Member
Unless it is a global variable it won't exist between rooms unless the object have persistent checked.. could that be the problem?
 

Carloskhard

Member
@TailBit
What is the exact code you're using to write to and read from the array?
The exact error is one of those two normally:
GML:
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_menu_tienda:

Variable obj_menu_tienda.icon(100184, 1) not set before reading it.
at gml_Object_obj_menu_tienda_Draw_64 (line 16) -        debug("bot: "+ string(icon[0,1]))
############################################################################################
--------------------------------------------------------------------------------------------
or
GML:
Push :: Execution Error - Variable Index [0,1] out of range [3,1] - -1.icon(100184,1)
at gml_Object_obj_menu_tienda_Draw_64 (line 16) -        debug("bot: "+ string(icon[0,1]))
My code is:

Code:
if(room=rm_tienda){
    //...
    debug("->"+ string(icon[0,1]))
    //...
}

else {
if(boton){
    room_goto_menu(rm_tienda);
    icon[0,1]=1;
  }
}
*Note that the DEBUG is script is just this: show_debug_message(string(object_get_name(object_index)) + ": " + string(argument[0]))
(This allows me to see which object is calling the debug message which is SUPER useful.)

Shoulnd'nt be any problem. And no, I don't have any other references to that array in the whole game,just that.
The strangest thing is that sometimes works, but then I change a number and it doesn't work again. It's so weird.
I'm thinking if this is some mistake I'm making with arrays or if it is a GM2 internal error.

Also this only seems to happen if I run this room after another level room, but I can't see how that affects, when clearly that array is only referenced on that object and that object is only call in that room.
This is ultra weird for me. I've spend 4 hours with this problem now.
 
Last edited:

Nidoking

Member
This may not affect anything, depending on what's in your room_goto_menu script, but what if you set the array value before that? Just swap those two lines.
 

Carloskhard

Member
This may not affect anything, depending on what's in your room_goto_menu script, but what if you set the array value before that? Just swap those two lines.
Oh right I forgot. That script just does this:
GML:
room_persistent = true;
audio_stop_sound(snd_propulsion);
room_goto(argument[0]);
and I've already tried setting that before but same problem.
 

Carloskhard

Member
Wait... are you calling room_goto_menu in the Draw event? Which events are these in?
In the draw event. That's because I'm drawing buttons inside there but also checking if they're pressed(IDK, GUI in GM is hard so I try different stuff).
Sometimes it works so its clear that draw events can define arrays, and also 100% sure they should be able to acces arrays.
Am I missing something obvious?

EDIT: I just checked and calling the debug line from step pop up the same error.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
[0,1] is out of the range of [1,1]. With a size of 1x1, you only have one cell accessible, which is [0,0]. Array indices are 0-based, size is 1-based. If one of the index digits in the error message are equal to one of the size digits, it's out of bounds.
 

Carloskhard

Member
[0,1] is out of the range of [1,1]. With a size of 1x1, you only have one cell accessible, which is [0,0]. Array indices are 0-based, size is 1-based. If one of the index digits in the error message are equal to one of the size digits, it's out of bounds.
Fir of all thank you the comments!!
I understand that, but you can see I declare the value for [0,1] perfectly. Why then it says that? Even worse, why is writting the correct value of that position in the output at the same time it says that is out of range? I don't see the logic.
([0,0]gets a value of 0 by default because of GM dinamyc arrays, but I've also tried declaring that manually for security but nothing).
 

Carloskhard

Member
UPDATE: I've also tried using just 1D arrays and the problem is the same. When I change room the arrays seems to dissapear.
I'm desperate to solve it and understand what is happening.
I repeat that this only happen if the same comes after another level room and I also repeat that the variables I'm using are not used anywhere else.
 
I would put a breakpoint on the line where I assign icon[0,1] = 1; and step through for a bit after that. I'm not at the computer right now and I can't exactly remember whether room_goto executes immediately or lets the current code block finish, but I would also try assigning 1 to the icon array BEFORE the room_goto command, just in case.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Whether room_goto halts execution or not should really be of no concern here. You should initialize and declare any indices of an array you're going to use before anything even has a chance to use them. Anything else is just asking for trouble - which you have run into, as you can see.

Even worse, why is writting the correct value of that position in the output at the same time it says that is out of range? I don't see the logic.
Both are correct. They're just not providing the same type of info.
In the error message "Variable Index [0,2] out of range [2,2]"...
[2, 2] refers to the size of the array. That is, it is 2 elements wide and 2 elements tall (or vice versa), for a total of 4 (2x2).
[0, 2] refers to the index, or cell, of the array you attempted to access. Unlike the size, this starts counting at 0, so the top-left cell is [0,0], not [1, 1].
 
Last edited:
Whether room_goto halts execution or not should really be of no concern here.
Well, I made a few assumptions:

1. The code is running in a persistent controller object.
2. rm_tienda is not visited until after boton is true, or to phrase it another way, that code first runs in a room that is not rm_tienda, botton is set to true, and that code is then used to go to rm_tienda.
3. This is the only code used to enter rm_tienda.

If what I assumed was right, then the execution of room_goto would matter. However, having thought it over more, I think it is more likely that some other code is sending the room into rm_tienda before boton is true and therefore icon[0,1] has not been initialised. So yeah, lol.
 

Carloskhard

Member
Whether room_goto halts execution or not should really be of no concern here. You should initialize and declare any indices of an array you're going to use before anything even has a chance to use them. Anything else is just asking for trouble - which you have run into, as you can see.


Both are correct. They're just not providing the same amount of info.
In the error message "Variable Index [0,2] out of range [2,2]"...
[2, 2] refers to the size of the array. That is, it is 2 elements wide and 2 elements tall (or vice versa), for a total of 4 (2x2).
[0, 2] refers to the index, or cell, of the array you attempted to access. Unlike the size, this starts counting at 0, so the top-left cell is [0,0], not [1, 1].
As I told you I know that range[1,1] means 1x1 and I DID declared variable [0,1] !
I did this:
GML:
icon[0,0]=0;
icon[0,1]=0;
(I tried doing that before and after the room goto).
So if I DID initialized that and declared it, why is saying that it doesn't exists?
And also, what do you mean with "They're just not providing the same amount of info."?
What is the part I'm missing?
 

Carloskhard

Member
Well, I made a few assumptions:

1. The code is running in a persistent controller object.
2. rm_tienda is not visited until after boton is true, or to phrase it another way, that code first runs in a room that is not rm_tienda, botton is set to true, and that code is then used to go to rm_tienda.
3. This is the only code used to enter rm_tienda.

If what I assumed was right, then the execution of room_goto would matter. However, having thought it over more, I think it is more likely that some other code is sending the room into rm_tienda before boton is true and therefore icon[0,1] has not been initialised. So yeah, lol.
1.Yes
2.Yes
3.Yes

No other code is accesing the room tienda(which means shop) and that line of code where the array is initialized is being executed since I saw it on debug mode and also put debug messages each step of the way and saw that the array info dissapear the momentI enter the room.

However if I declare the info on the create event it works, and if I put the room in other place it also works.
None of that makes sense to me.
1D Arrays don't work either.
I've tried creating new variables just to be sure and the error is the same. I'm lost
 
Try using a breakpoint and debugging from there. Sometimes the only way to find a bug is to manually track it down by stepping through the code. If you're sure that you're setting icon[0,1] before you enter rm_tienda, then you'll just have to follow it and see what happens to it.
 

Carloskhard

Member
Try using a breakpoint and debugging from there. Sometimes the only way to find a bug is to manually track it down by stepping through the code. If you're sure that you're setting icon[0,1] before you enter rm_tienda, then you'll just have to follow it and see what happens to it.
I've already done that, however I don't really know how to see the content of position in an array.
I've tried using the watch window and "obj_menu_tienda.icon" and "obj_menu_tienda.icon[0,0]"(Which I know it should work since it works) but it always says "<unable to evaluate>" , however the debug message its writting it correctly.
In this screenshot I've created the array called "array" just a test so you can see it:

Captura de pantalla (229)_LI.jpg
 
Last edited:
Ok, just to make sure, you set a breakpoint at:
Code:
else {
if(boton){
    room_goto_menu(rm_tienda);
    icon[0,1]=1; // This line
  }
}
And then you've pressed "stepped into function" (F11) through the code lines one at a time until the code got to this part:
Code:
if(room=rm_tienda){
    //...
    debug("->"+ string(icon[0,1])) // When this is triggered
    //...
}
And seen all the lines of code that run between?
 

Carloskhard

Member
Ok, just to make sure, you set a breakpoint at:
Code:
else {
if(boton){
    room_goto_menu(rm_tienda);
    icon[0,1]=1; // This line
  }
}
And then you've pressed "stepped into function" (F11) through the code lines one at a time until the code got to this part:
Code:
if(room=rm_tienda){
    //...
    debug("->"+ string(icon[0,1])) // When this is triggered
    //...
}
And seen all the lines of code that run between?
That's correct and haven't seen anything.
Also I don't understand why from the beggining I can't see the value of the array fromthe watch window. Would be much easier if that worked and I think I have to start there.
If I declare "array[1]=1" thenright after that I should be watching the value in the window"watch" but I don't.
Am I missing something?
I fear this is a GM internal problem
 

TsukaYuriko

☄️
Forum Staff
Moderator
You're missing that the Watches window merely asks for variable names, not a full object.variable reference. So your watch should be array. If the instance currently being stepped through has a variable named array, its contents will be displayed there.
 

Carloskhard

Member
You're missing that the Watches window merely asks for variable names, not a full object.variable reference. So your watch should be array. If the instance currently being stepped through has a variable named array, its contents will be displayed there.
That's not correct completely.
If you add the name of the object, the watch will always display correcly the info of the variable no matter where you are stepping, so this way is more accurate.But yeah, just for science I've tested it without the name of the object and the result is the same.
There is something obviously wrong since the watch isn't displaying anything even when I just run that line of code declaring the array.
 
Last edited:
If you look at the bottom of the debugger, there's multiple panels like Variables, Graphics, Instances, etc. If you click on Variables, you can see the actual variables being set in real time by using the "stepped into function" button or pressing F11. If you open the ".Self" submenu thingy in the Variables window, you can see the instance variables being assigned to that current instance with whatever line of code the debugger is waiting on. If the variable is outside of the ".Self" or ".Other" menus, then it is a local variable and will be cleared when that event is finished. I'm curious as to what happens to icon as you step through the code, do you ever see it appear in the ".Self" or as a local variable underneath that? If it does appear, what instance is running that code? You can see the instance ID and name to the right of ".Self". When you finally get to wherever the if (room == rm_tienda) part, is the icon variable within the correct scope there? Is it in the variables window at any point in rm_tienda before it is accessed? Has it's value been changed? Etc...
 

samspade

Member
As a starting point, if game maker says something like "variable index out of range" then it is.* So the question should be: Why is the array variable index out of range when I think it shouldn't be? And that isn't just picking on the language but an important shift in how you think about the problem and (therefore) solution.

When I change room the arrays seems to dissapear.
I repeat that this only happen if the same comes after another level room and I also repeat that the variables I'm using are not used anywhere else.
However if I declare the info on the create event it works, and if I put the room in other place it also works.
This seems to be somehow related to the problem. Given the screen shot showing that the debugger is unable to evaluate the variable and these statements it seems likely that something is happening somewhere to mess up the array variable. But it also makes it harder for someone on this end to debug.

It would be annoying, but probably the fastest solution to try the following:
  • Remove as much code from the project as possibly while still causing the error.
  • Put a break point right before the room transition code.
  • Run the game in the debugger
  • Trigger the room transition code
  • Ensure that the array variable exists as expected immediately before the room transition (I would do this both with the debugger and a line in code like show debug message or even just trying to access the variable and saving the result to a temporary variable for verification)
  • Step through the code, using the step in function, until you reach the line of code that is causing the problem (so watching the variable in the debugger at every line). Do NOT use another break point. This way you can see all of the code, and its order, between when you know the problem doesn't exist and when it does. If you're stepping through it line by line, you should be able to pinpoint the exact instance it occurs.

As a side note for the debugger, I don't use the watch panel very much, I normally use the selected instance or all instances panel.


*yes, in theory you could have found a very strange bug, but the likelihood of finding a bug on something as common as accessing an array is virtually 0%. And to confirm that it was a bug you would need to make it reproducible with as little code as possible in a separate project so that it could be reported, which would also require you to pinpoint the exact point and reason for the bug.
 

Carloskhard

Member
If you look at the bottom of the debugger, there's multiple panels like Variables, Graphics, Instances, etc. If you click on Variables, you can see the actual variables being set in real time by using the "stepped into function" button or pressing F11. If you open the ".Self" submenu thingy in the Variables window, you can see the instance variables being assigned to that current instance with whatever line of code the debugger is waiting on. If the variable is outside of the ".Self" or ".Other" menus, then it is a local variable and will be cleared when that event is finished. I'm curious as to what happens to icon as you step through the code, do you ever see it appear in the ".Self" or as a local variable underneath that? If it does appear, what instance is running that code? You can see the instance ID and name to the right of ".Self". When you finally get to wherever the if (room == rm_tienda) part, is the icon variable within the correct scope there? Is it in the variables window at any point in rm_tienda before it is accessed? Has it's value been changed? Etc...
As a starting point, if game maker says something like "variable index out of range" then it is.* So the question should be: Why is the array variable index out of range when I think it shouldn't be? And that isn't just picking on the language but an important shift in how you think about the problem and (therefore) solution.





This seems to be somehow related to the problem. Given the screen shot showing that the debugger is unable to evaluate the variable and these statements it seems likely that something is happening somewhere to mess up the array variable. But it also makes it harder for someone on this end to debug.

It would be annoying, but probably the fastest solution to try the following:
  • Remove as much code from the project as possibly while still causing the error.
  • Put a break point right before the room transition code.
  • Run the game in the debugger
  • Trigger the room transition code
  • Ensure that the array variable exists as expected immediately before the room transition (I would do this both with the debugger and a line in code like show debug message or even just trying to access the variable and saving the result to a temporary variable for verification)
  • Step through the code, using the step in function, until you reach the line of code that is causing the problem (so watching the variable in the debugger at every line). Do NOT use another break point. This way you can see all of the code, and its order, between when you know the problem doesn't exist and when it does. If you're stepping through it line by line, you should be able to pinpoint the exact instance it occurs.

As a side note for the debugger, I don't use the watch panel very much, I normally use the selected instance or all instances panel.


*yes, in theory you could have found a very strange bug, but the likelihood of finding a bug on something as common as accessing an array is virtually 0%. And to confirm that it was a bug you would need to make it reproducible with as little code as possible in a separate project so that it could be reported, which would also require you to pinpoint the exact point and reason for the bug.
Thanks for the long answer(To you and to @RefresherTowel and @TsukaYuriko).

To answer you, I have already done all of the points exept the first one, which I still could erase a lot of code but this is a 2 year game so it could take a lot of time.

I've put a break before transitioning rooms, however we've already seen that the problem is happening from the beggining since the watch can't see the values of the array not even right after running a simple line like "array[1]=0" which doesn't live any or not space at all for other lines interfering.
Due to this, I can't run trough the code to see where is failing. I should solve why the watch is not displaying the array.

Also answering to @RefresherTowel ,I can't see the "variables" panel. Maybe I'm missing the obvious.Do you see that panel here or do I have to add it somehow?


Captura de pantalla (227).png
 

samspade

Member
Also answering to @RefresherTowel ,I can't see the "variables" panel. Maybe I'm missing the obvious.Do you see that panel here or do I have to add it somehow?
I can't remember how the default debugger is set up anymore. To get the various panels, while the debugger is running you have a debugger drop down menu up top (as shown in your picture). Click on that and then select the various panels that you want. You can also move the panels around in the tab at the bottom and even have multiple panels in the same tab.

As to removing code, you just need to remove enough so that stepping through every line without stopping is viable. This should be relatively easy, even for larger projects as you should be able to deactivate instances or comment out code that isn't necessary. While this isn't super helpful at this point, in general your project should be designed in such a way that is possible. If it is difficult to remove instances and code and so one then that is a sign that too many things depend on too many other things which increases the likelihood of issues like this.

For example, in this case, it seems like you have a shop or store object with an inventory and a room which presumably is where this shop or store is and that it only occurs with specific room ordering. You should be able to remove almost every else then but a few rooms and one object.
 

TailBit

Member
Try print the id before setting and before reading it, just to make sure it happens in the same instance?

As there being two could be the reason the debug couldn't find anything. (just a shot in the dark)
 

TheouAegis

Member
Open the debugger, click on the main IDE window, then when the "Debugger" menu appears at the top (it's in your screenshot), click on it and pick "All Instances". Pause the game (or get to a breakpoint) and that window will list all the instances. Click on the + symbol to see a specific instance's variables.
 

Yal

🐧 *penguin noises*
GMC Elder
Variable index[0,1] out of range [1,1] - - 1.array[100184,1]
The range is the number of cells, so since the array has range 1x1... the only cell that has a value is 0,0. So the code that sets 1,0 hasn't been run yet when that error occurs.
 

Carloskhard

Member
Try print the id before setting and before reading it, just to make sure it happens in the same instance?

As there being two could be the reason the debug couldn't find anything. (just a shot in the dark)
Wow! This was it!
So I spend a lot of time deleting objects but the core manager of the game was checking every new room if the object of the shop existed and if not, creating another one. The problem was that the code for checking if the shop existed was missing the "!" NOT statement,so every new game room a new shop object was being created. Thus, the array was being creted only in one of those objects, which was leading to the watch getting confused when I used "obj.array"(Because there was many instances of the same type,therefore couldn't print just one)and when using just "array", the data was not being "deleted" but the new object stepping was another one and I wan't being aware of that(Since the debugger doesn't open a new panel for the same event of the same object/diferent instances.
That was also why creating the array in create event was working.

So yeah, thanks a lot for all the great help and debugging tips! You're amazing guys!
Here's the shop opening up correctly thanks to you help :)
 
Last edited:
Top