Need some help with the "out of range" error ( I'm loving and hating arrays at the same time) || warning: [bad english inside]

clooms

Member
Hello, and thank you for reading this.
I'm searching for any help to understand this problem

I have some issues with an array I made in a "Oconsole" object to be consulted from others objects events.

I initiated it this way (Object: Oconsole | Event Create)

_______________________

for(var i=0;i<7;i++)
{
for(var j=0;j<7;j++)
{ ship_array[i,j] = 0 }
}

ship_array[3,3]=1
ship_array[3,4]=1

_______________________

Whene I try to check some values inside this array from an other object I got an error

Here is the code that check the value: (Object OShip | Event Create)

_______________________

if instance_exists(Oconsole)
{
if Oconsole.ship_array [3,3] == 1
{ instance_create_layer(x,y+64,"instances",Oship_couloir) }
}
_______________________

Here is the error message I got from it:

_______________________
############################################################################################
ERROR in
action number 1
of Create Event
for object OShip:
Push :: Execution Error - Variable Index [3] out of range [0] - 6.ship_array(100061,3)
at gml_Object_OShip_Create_0 (line 30) - if Oconsole.ship_array [3,3] == 1 ############################################################################################ gml_Object_OShip_Create0 (line 30)
________________________

The weird thing about this is that I can check any value from this array since I do it from the original object ( the Oconsole object).

I have tryed to draw all the values in game to see if it works. And it does.

Here is the code and the result:

________________________
for ( i=0; i<7 ;i++)
{
for (j=0; j<7; j++)
{draw_text(x-250+(i*16),y-100+(j*16),ship_array[i,j])}
}

array.jpg
_______________________

I can't figure how the value from the same array can be out of range from one object and not from an other.

Well I am out of solutions and I tryed many many things to make it work.
If you have any idea I would be so glad to read it.

Thank you again for reading it all.
 

CruelBus

Member
The only difference I see is that there is a SPACE in the middle of:
if Oconsole.ship_array [3,3] == 1
______________________^ right there

and everywhere else it is ship_array[3,3] with no space.
I'm not sure if that makes a difference here or not.


As far as using a 2D array like that, I just finished changing my game to a different array format. Using my_array[x,y] still works for compatibility, but I'm not sure for how long. You might want to look into setting up your 2D arrays differently to future-proof your game to some degree. I converted all of mine to 1D arrays.
 
As well as @CruelBus's suggestion, I would make sure that Oconsole is being created before OShip runs its create event, otherwise, you're trying to access an array that hasn't been created yet (I feel like it should return an undeclared variable error in that case, rather than the out of bounds error, but who knows, certainly not me as I'm too lazy to test).
 

clooms

Member
Hey CruelBus!
thank you for your comment.
I almost tryed everything with space and no space combinations with no result. but I got the same idea at start.

Thank you RefresherTowel
I already checked that. The Oship object is created by an Oconsole alarm, 2 frames after the Oconsole create. (I made the alarm command inside the Oconsole create, after the array creation)
Also there is a condition in the code I show up there that check if Oconsole object exist.
And the error specify "out of range". not the "Not set before reading this"

Thank you guys for taking time.
 

chamaeleon

Member
Search your project for any use of ship_array and see if it is being misused anywhere. I wouldn't be surprised if you have an assignment using only one index instead of both, turning that entry into a single value instead of an array.
GML:
var a;
for (var i = 0; i < 7; i++) {
    for (var j = 0; j < 7; j++) {
        a[i,j] = 0;
    }
}

a[3] = 0;

if (a[3, 3] == 0)
    show_debug_message("Yes");
else
    show_debug_message("No");
Code:
Variable Index [3] out of range [0]
 at gml_Object_obj_generic_Create_0 (line 10) - if (a[3, 3] == 0)
 

clooms

Member
I will try to change the name of my array as Chamaeleon suggest.
there are things I dont understand in the code Chamaeleon shared and in your first suggestion.

the expression: "a[3] = 0"
Since "a" is a 2d array I dont understand the meaning of it.

also I never used a debug message. I ll have to learn how it work.

also
I m not sure to understand your point whene you suggested the format: my_array[x,y]
you are still talking about the way to write it right? about spaces etc...

I am not very optimistic, from memory there is not any other occurency of this array in the all code. ( be sure I will check in all way possible )
I dont think the ship_array is changed by something else cause I can draw it perfectly at every frame for hours with no problem, since I dont check the values from an other object.
Or can you change the array range and keep the values the same?

If my code work on your quick project ( the same code worked for me in about 15 others projects) maybe the file is corrupted or something. I started this 3 Updates away.

Thanks a lot guys. Your help is much appreciated.
 

CruelBus

Member
What I mean is: ship_array[i,j] is OK but it is old method, not a bug.

I use this method for a 2d array, converted to a 1d array:
Code:
var i=6;
repeat(i+1){
    var j=6;
    repeat(j+1){
        ship_array[(i*6)+j]=0;
        --j;
    }
    --i;
}
var shipX=3,shipY=3;
ship_array[(shipX*6)+shipY]=1;
shipY=4;
ship_array[(shipX*6)+shipY]=1;
It looks a little crazy though.
 

chamaeleon

Member
I will try to change the name of my array as Chamaeleon suggest.
I'm not suggesting any name changes at all, there's nothing wrong with using the variable name you are using now, if it satisfactory to you.
the expression: "a[3] = 0"
Since "a" is a 2d array I dont understand the meaning of it.
2d arrays in GMS are now 1-d arrays of 1-d arrays, not a particular data type that is a 2d array. So what one assignment does is make that particular array entry a number instead of an array (just like a=[1, 2, 3]; a="b"; would first make a contain a array, then a string, only my example is operating on a specified array entry instead of an individual variable), while the other entries remains arrays (unless affected in the same way by similar operations, perhaps in a loop).

GML:
var a;
for (var i = 0; i < 7; i++) {
    for (var j = 0; j < 7; j++) {
        a[i,j] = 0;
    }
}
a[3] = 1;
show_debug_message(a);
Code:
[ [ 0,0,0,0,0,0,0 ],[ 0,0,0,0,0,0,0 ],[ 0,0,0,0,0,0,0 ],1,[ 0,0,0,0,0,0,0 ],[ 0,0,0,0,0,0,0 ],[ 0,0,0,0,0,0,0 ] ]
Note the 4th position now being a number and not an array like the rest of the entries.

Ctrl-Shift-F or Edit->Search & Replace and search for ship_array. If nothing comes up, another possibility is that you may pass it as an argument to a function and there's a @ accessor being used with a single dimension instead of 2 with the intent of modifying content, but using a different variable name so it wouldn't come up in a search (except in the call location of course) for the original variable name.
 
Last edited:

nicognito

Member
If you've never used the show_debug_message function, I'd also recommend that you use the GameMaker debugger and put breakpoints at relevant location. If you've never used a debugger, this is a powerful tool to debug your code, and understand what's happening, without necessarily cluttering your code with debug messages šŸ˜Š (there are probably tutorials online about how to use the GM debugger).

I don't know if every versions of GameMaker support it, but also consider using try/catch statement around the faulty code, and put a breakpoint in the catch block.
 

clooms

Member
Okay thank you Cruelbus I had absolutly no idea that method existed.
Whene I replace my array creation by your the game react differently.

_With your method the array can't be red by any event. the out of range message appear at the first frame.
_With mine (the old way) the Oconsole object is able to read it but not any other object...

Look like a bug to me...


Thank you to Chamaeleon It's realy usefull for me to know.
Now I see clearly what your code do, but I dont understand the point of doing so. (it gave me the same error message (out of range))

After verifications and a few more tests; there are not any other occurency of "ship_array" in the entier project. I also changed the array name by an other( that I am totaly sure I never used in my entier life) with no result.

At this point and if you have no more idea;
I think I ll try to restart a project from zero and copy-plast every events and script... (Sure it will be a lot of fun :D)
Thank you again for taking time. I learned things.
 

clooms

Member
Hello nicognito.
Thank you for reading and comment.
Actualy I never saw any interest of using "show_debug_message" instead of a draw event. I still don't understand the point but you are right I don't know how to use the debugger so maybe that's why. I ll check for a tuto.
 

chamaeleon

Member
Now I see clearly what your code do, but I dont understand the point of doing so. (it gave me the same error message (out of range))
The point of my code is to show how the error can occur in a small standalone example, with the idea that you investigate your code for similar mistakes. I was simply trying to come up with a sample code that generated the same message to show what the mistake would look like.
 

chamaeleon

Member
Ok, different test
GML:
if (an_undefined_variable[0, 0] == 0) {
    show_debug_message("Foo");
}
Code:
Push :: Execution Error - Variable Index [0] out of range [0] - -1.an_undefined_variable(100125,0)
at gml_Object_obj_generic_Create_0 (line 1) - if (an_undefined_variable[0, 0] == 0) {
Given this test, without initializing the variable at all, I am somewhat surprised to get the same message. So another issue you may face may simply be an order of operations, where some code tries to use the variable before it has been created and initialized.

In contrast, the error message I would have expected
GML:
if (an_undefined_variable[0] == 0) {
    show_debug_message("Foo");
}
Code:
Variable obj_generic.an_undefined_variable(100125, 0) not set before reading it.
at gml_Object_obj_generic_Create_0 (line 1) - if (an_undefined_variable[0] == 0) {
 

clooms

Member
I copy plasted, object by object, the exact same code and it's now working.
I guess my previous file was corrupted.

I red your comment and I see how you investigate. ( I'm a beginer and english is not my favorite language so I had to read it twice and think about it for a moment.)
If I understand the way you roll. You are doing something of a scientific methode applied on the programm instead of physics

I will keep it in mind cause I think it's a good advice and an efficient way to be sure of things.

Thank's a lot.
 
Last edited:
Top