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

Legacy GM SOLVED: Global Array Reading Problem

P

Pelican

Guest
I'm getting some very strange things happening with an array right now, and was wondering if anyone would know what is going on.

I have an array (global variable) called targetChara, pertaining to target character information. The array has been set up as shown, in a function called checkTargetSpace(y,x):
Code:
targetChara[0]=self.name;
targetChara[1]=self.level;
targetChara[2]=self.class;
targetChara[3]=self.xPos;
targetChara[4]=self.yPos;
...
Code:
targetChara[0]=self.name;
targetChara[1]=self.level;
targetChara[2]=self.class;
targetChara[3]=self.xPos;
targetChara[4]=self.yPos;
targetChara[5]="-";
targetChara[6]="-";
targetChara[7]=self.living;
targetChara[8]=self.mobile;
targetChara[9]=self.moved;
targetChara[10]=self.able;
targetChara[11]=self.acted;
targetChara[12]=self.currentHP;
targetChara[13]=self.maxHP;
targetChara[14]=self.currentSP;
targetChara[15]=self.maxSP;
targetChara[16]=self.atk;
targetChara[17]=self.def;
targetChara[18]=self.mag;
targetChara[19]=self.res;
targetChara[20]=self.spd;
targetChara[21]=self.eva;
targetChara[22]=self.lck;
targetChara[23]=self.walk;
targetChara[24]=self.jump;
targetChara[25]=self.weapon;
targetChara[26]=self.armour;
targetChara[27]=self.head;
targetChara[28]=self.access1;
targetChara[29]=self.access2;
targetChara[30,0]=self.items[0];
targetChara[30,1]=self.items[1];
targetChara[30,2]=self.items[2];
targetChara[30,3]=self.items[3];
targetChara[30,4]=self.items[4];
targetChara[31]=self.ability1;
targetChara[32]=self.ability2;
targetChara[33]=self.skills;
targetChara[34]=self.experience;
targetChara[35]=self.experienceNeeded;
targetChara[36]=self.description;
targetChara[37]=self.alignment;
targetChara[38]=self.ability1Level;
targetChara[39]=self.ability2Level;
targetChara[40]="";
targetChara[41]=self.hadTurn;
for(i=0;i<array_length_1d(self.status);i++){
selectedChara[42,i]=self.status[i];
}
targetChara[43]=self.facing;
However, for some reason when I reference this in a later piece of code:
Code:
if(targetChara[42,2]==1){
Do things...
}
The code shows me this error:
Code:
Push :: Execution Error - Variable Index [42,3] out of range [31,-1] - -5.targetChara(100021,1344003)
Which I don't understand, since there's definitely more than 31 array slots...

Additionally, when testing the array as follows:
Code:
for(i=0;i<array_height_2d(targetChara);i++){
                        for(j=0;j<array_length_2d(targetChara,i);j++){
                            show_message("targetChara["+string(i)+","+string(j)+"]: "+string(targetChara[i,j]));
                        }
                    }
The result comes out at [0,1],[0,2][0,3] rather than the usual way, then once it gets to [0,43] jumps to [30,0],[30,1],[30,2],[30,3],[30,4], then stops...

Any idea what might be causing it? I'm pretty stumped...
 

TheouAegis

Member
Dude, according to your code targetChara is a 1D array. Why the hell are you trying to read it as a 2D array? You have to set it up as a 2D array if you want to read it as a 2D array!

Your only 2D array is selectedChara, not targetChara.
 
P

Pelican

Guest
Dude, according to your code targetChara is a 1D array. Why the hell are you trying to read it as a 2D array? You have to set it up as a 2D array if you want to read it as a 2D array!

Your only 2D array is selectedChara, not targetChara.
I'm reading it as a 2D array based on this part:
Code:
targetChara[30,0]=self.items[0];
targetChara[30,1]=self.items[1];
targetChara[30,2]=self.items[2];
targetChara[30,3]=self.items[3];
targetChara[30,4]=self.items[4]
So I assumed that meant the entire thing was a 2D array?
 
P

ph101

Guest
You cant define it as 1d and then switch to also treating it as 2d array like you seem to be doing here:

Code:
targetChara[29]=self.access2;
targetChara[30,0]=self.items[0];
At every point you should be defining a 2d array like this :

Code:
targetChara[val1,val2]  = value //how to reference a 2d array
and never also

Code:
targetChara[val1] = value //how to reference a 1d array
which is probaly causing the strange results.

I should add, I'm not sure what you are doing with the self there? You shouldn't need to be using that, the calling instance would access it's own variable..also, the manual says you should never need to use this keyword.
 
Last edited by a moderator:
P

Pelican

Guest
You cant define it as 1d and then switch to also treating it as 2d array like you seem to be doing here:

Code:
targetChara[29]=self.access2;
targetChara[30,0]=self.items[0];
At every point you should be defining a 2d array like this :

Code:
targetChara[val1,val2]  = value //how to reference a 2d array
and never also

Code:
targetChara[val1] = value //how to reference a 1d array
which is probaly causing the strange results.

I should add, I'm not sure what you are doing with the self there? You shouldn't need to be using that, the calling instance would access it's own variable..also, the manual says you should never need to use this keyword.
Ah, okay. Thank you. I'll try and alter my code to see if it works. :)
The self thing is because that whole assignment code is inside a with() statement pertaining to an object already in the room. So it's taking the variables from that object. :)
 
P

ph101

Guest
Ok, good luck.
The self thing is because that whole assignment code is inside a with() statement pertaining to an object already in the room. So it's taking the variables from that object. :)
If it is inside a with statement, you also don't need to use self. All the code is executed as if from the instance specified after with. So.. you just use the variable name. You might want to use other.. to reference back to the calling instance as it were, but not self...
 
P

Pelican

Guest
Just tried it on my computer, it worked alright now! Thanks for the advice, now to spend some time putting zeroes into all my arrays. :p

I'll keep that in mind. I'm still a bit shaky with going from Javascript to GML, but I seem to be getting there. Thanks for the help. :)
 
Top