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

(solved) Trying to index a variable which is not an array?

spe

Member
I'm getting an error in my prototype CCG and I can't seem to figure out what the cause of it is. The error information seems unusually unhelpful.

FATAL ERROR in
action number 1
of Create Event
for object obj_card:


trying to index a variable which is not an array
at gml_Object_obj_card_Create_0 (line 1) - {

It happens whenever I select an instance to turn into another instance within the game. I've tried looking up this error, but all the other solutions I've found seemed to have had something to do with the array not being created before being used. I've moved the control object (which I have used to initialize the arrays) into an 'initialization' room by itself, that is the first room in the game, so I don't think the array doesn't exist before being used. I've checked for typos probably twenty times, so I doubt that's it. I've tried making the arrays global, that hasn't helped. I'm not sure what else to try. Any help is appreciated.
 

Bingdom

Googledom
Can you show us relevant code? and possibly the full error log? ;)

EDIT: Also, i think this should be in the programming section.
 

spe

Member
Yeah, I wasn't sure if it should have gone in programming or tech support, because I'm not sure if error codes are considered 'technical issues' or not (I think they are, though...)
EDIT: Apparently 'technical issue' in this context excludes coding errors? Whatever, someone can move this thread to the proper place if they want, I don't think I'm allowed to. :p


I'm pretty sure I didn't leave anything important out of the error log, but here is the full copy anyways:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object obj_card:

trying to index a variable which is not an array
at gml_Object_obj_card_Create_0 (line 1) - {
############################################################################################

I hope those pound signs and underscores help reveal my issue. :p

Anyways, here's the bit of code mentioned in the error message (obj_card_Create_0). Keep in mind, I only have a few days of experience in GML, so my coding might not be very good. :p

///Select the card

position = obj_card_slot.position;
empty = false;

card_id = obj_card_slot.card_selected;
card_name = obj_control.card[card_id, NAME];
card_attack = obj_control.card[card_id, ATK];
card_defense = obj_control.card[card_id, DEF];
card_sprite = obj_control.card[card_id, SPR];


I've tried using the arrays without the 'obj_control.' before, and I've also tried making them global, and both simultaneously. Nothing helped.

BTW, if you couldn't tell, I'm using 2D arrays, and the all-caps variables are macros assigned to a number meant to represent the horizontal position in the array used to store that specific value for the specific card, which is stored using its unique card ID in the vertical section of the array. This is my first time coding anything in GML on my own, I've done a bit of coding before only following tutorials, so this whole thing is kinda complicated to me and I know I have probably made a rather simple mistake somewhere.
 
Last edited:

Yal

🐧 *penguin noises*
GMC Elder
The issue seems to be that the card array in the control object doesn't get initialized properly (to an array). Do you create enough data for at least two cards? I've heard that there can be issues addressing an array with a length of 1 along either axis.
 

spe

Member
Yeah, I thought the problem might have something to do with the initialization, although I have created three rows and five columns. I've tried looking up proper syntax for 2D arrays, and it looked fine to me, but here it is if you think there might be anything wrong with it.

///Initialize Global Variables

//0 = ID
//1 = NAME
//2 = ATK (attack)
//3 = DEF (defense)
//4 = SPR (sprite)

//initialize all cards

//default card
card[0, ID] = 0;
card[0, NAME] = "";
card[0, ATK] = 0;
card[0, DEF] = 0;
card[0, SPR] = 0;

//card 1

card[1, ID] = 1;
card[1, NAME] = "Infantry";
card[1, ATK] = 50;
card[1, DEF] = 250;
card[1, SPR] = spr_infantry;

//card 2

card[2, ID] = 2;
card[2, NAME] = "Archer";
card[2, ATK] = 30;
card[2, DEF] = 320;
card[2, SPR] = spr_archer;

//card 3

card[3, ID] = 3;
card[3, NAME] = "Mage";
card[3, ATK] = 70;
card[3, DEF] = 180;
card[3, SPR] = spr_mage;



The main reason I've opted to used a 2D array is simply to store all the permanent card values, which is why I initialize them like this rather than with a for loop setting everything to 0. The data there is mostly arbitrary; I've only tried to access the ID and sprite information elsewhere in the code so far.
 

Yal

🐧 *penguin noises*
GMC Elder
Hmm... and there's nowhere else you accidentally overwrite card?

Here's an idea. Right before the code that triggers the error, put this.
Code:
show_debug_message(obj_control.card)
If card is a 2D array, its contents will be printed between curly brackets. If it's NOT an array, it'll be printed as a singular value... and then that's the problem.
 

spe

Member
I'm pretty sure this is the only time the array is being mentioned, so I don't think it's been overwritten. Anyways, here's what I got:


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object obj_card:

Variable obj_control.<unknown variable>(100006, -2147483648) not set before reading it.
at gml_Object_obj_card_CreateEvent_1 (line 3) - show_debug_message(obj_control.card)
############################################################################################

I get the impression that wasn't the intended result.
 

spe

Member
Ah, I seem to have found it. Turns out, I had initially tried changing the create event of my control object to a game start event, hoping it would somehow prioritize the order of the array being created, but I never changed it back. It seems odd that it wasn't working just because of this, as it was in its own room before the main room, before any of the card slots were even created, well after the actual start of the game. Anyways, I've changed it back to a create event, and everything works fine. I'm honestly surprised the rest of my code works fine too, I was almost certain I'd run into another problem. :p Anyways, thanks for the help. Like I predicted, it was just some simple mistake. Guess I just had to keep combing through it until noticing the problem.
 

TheouAegis

Member
Was your control room not the first room of the game? If the control room wasn't the very first room accessed, then the Game Start event will never run. The Game Start event only runs from the very first room in the resource tree.

Also Game Start and Room Start events are the last events run at the start of a room, just in case you still didn't know.
 

spe

Member
Yes, actually, the control room was the first, which is why I'm confused as to why it wasn't working. I changed nothing except the Game Start event to a Create event, the control room was always the first one in the resource tree. Maybe it was because the creation code for the room said goto_next at the end? Does the creation code run before the Game Start event? Just wondering, for future reference.
 

Bingdom

Googledom
Oops, i didn't notice your first half of the error log. I thought you only brought up this:
trying to index a variable which is not an array
at gml_Object_obj_card_Create_0 (line 1) - {
To answer your question, yes, the creation code runs before the game start event. You can test this by doing

global.points = 10 in the room creation code

global.points = 5 in the game start event

When you draw the result, it will end up being 5, because the game start event activates first, then game start.
 

spe

Member
Okay then, that explains why it wasn't working. The array was never initialized because the creation code started the next room before the object in the room could run the game start event, which contained the array initialization. I get it now. Thanks :)
 
Top