• 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 How to call array in array?

L

Luxxum

Guest
Hey, I was trying to make a global array by using:


global.arrayname = [ [11,12,13], [21,22,23], [31,32,33] ]


and gamemaker doesn't seem to mind it, but when I try to call it via:


variable = global.arrayname[variable2[real_number]]


it isn't working. Could you tell me what I'm doing wrong?

the exact error message is:
-----------------------------------
FATAL ERROR in
action number 1
of Create Event
for object moving_block_vertical_basic:

trying to index a variable which is not an array
at gml_Object_moving_block_vertical_basic_Create_0 (line 18) - yspeed = global.vblock[vblockn[2]]
----------------------------------



Thanks
 

CMAllen

Member
Do you need to deal with nested array structures? Can you not simply get the nested array's specific reference number and work with the array directly?

ex:
Code:
temp_array = global.arrayname[variable2]
varialbe = temp_array[real_number]
 
L

Luxxum

Guest
Do you need to deal with nested array structures? Can you not simply get the nested array's specific reference number and work with the array directly?

ex:
Code:
temp_array = global.arrayname[variable2]
varialbe = temp_array[real_number]
No, i'm getting a different error now:

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

Push :: Execution Error - Variable Index [0,3] out of range [1,3] - -1.vai(100036,3)
at gml_Object_moving_block_vertical_basic_Create_0 (line 20) - yheight = vai[3]


I think I am misunderstanding how arrays work in GM2. I've only really used matlab before this, so I probably am making some false assumptions.

Thanks though!
 

salyossy

Member
Do you need to deal with nested array structures? Can you not simply get the nested array's specific reference number and work with the array directly?

ex:
Code:
temp_array = global.arrayname[variable2]
varialbe = temp_array[real_number]
it worked for me! thanks to your solution, now i can use nested arrays
 
Your initial error was a misunderstanding of arrays. To get access to the data contained in an array position, you have to supply a number, so global.vblock[vblockn[2]] will not work (I mean, it could if vblockn[2] returns a number in this scenario, but I doubt that is what you were intending). CMAllen has given the correct answer, first you have to pull the inner array out of the position it is stored in inside the outer array. So if you want the inner array stored in position 0 in the outer array, you would go:
Code:
var temp_arr = global.vblock[0];
// Now the temp_arr variable holds whatever array was stored in global.vblock[0]
var my_var = temp_arr[2] // This pulls whatever is stored in position 2 from the array stored in position 0 in global.vblock
The error you are getting after trying to do vai[3] is actually caused because the vai array is not 4 indices long (0, 1, 2, 3). Remember all arrays/lists/etc start at position 0. So if you have 3 pieces of data stored in the vai array, they would be stored at position 0, 1 and 2. It's likely that you actually want to access the third thing you added to the vai array, which you would get by calling vai[2], since it would go vai[0], vai[1] and then vai[2] for the third piece of data.
 
O

OccultOne

Guest
I think you can use: array_get(global.vblock[0], 2) for a single line method of accessing nested arrays. I do something similar with ds_maps
 
Top