Legacy GM SOLVED: Small 2D Array Issue

S

Silversea

Guest
I usually use 1D arrays, so apologies for what seems like a stupid problem. The solution is probably easy...

The error is
"Variable Index [1,2] out of range [301,0] - - 1.row(10045,32002)"

My code is simply:


Create event:
Code:
row[300,2] = 0
Draw event:
Code:
draw_sprite(row[1,1],row[1,2],x,y)
What have I done wrong?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Each row of 2d array is a separate 1d array, meaning that you'd have to do
Code:
row[300,2] = 0;
for (var i = 0; i < 300; i++) row[i,2] = 0;
or you will end up with array that has 300 empty rows and one that houses 3 elements.
 
S

Silversea

Guest
Aha! That's what debug was showing me but I must admit I assumed it entirely worked like a 1D array.

That's as in defining variable[7] = 0 would also create the previous array values automatically. Thanks for this! I guess that makes sense. Not everyone wants multiple sectors for every line of their 2D array.

PS. I wish this was explained in the manual though! Seems like important information...
 
Last edited by a moderator:
Top