GML [SOLVED] Problem with getting instance ID's from an array

F

frootloop

Guest
Hi there,

Apologies if this is obvious - I couldn't find an answer googling - I suspect I'm not asking the right question.

I'm creating an array of object instances (aliens for a space invaders type game). When I read back the instance id's from the array, the first few seem right but then the rest are just zero. I can't seem to figure out why. Any help would be appreciated!

My create event:
Code:
// start position for the formation
fxpos = 30
fypos = 30

//temp vars - will use these to calculate positions of next alien
curx = fxpos
cury = fypos

//vertical and horizontal offset between aliens
invwidth = 32
invheight = 32

//how many aliens?
frows = 5  //how many alien rows in formation
fcols = 5  // how many columns in the formation
arraysize = frows * fcols
//create the array to hold the alien object instances
fidArray = array_create(arraysize)

for (var i = 0; i < frows; i++)   // for each row
{
    for (var j = 0; j < fcols; j++) //lets create an alien instance for each column in the row
    {
       
   
                fidArray[i]=instance_create_depth(curx, cury, 0, objInvader);
           
               
        curx = curx + invwidth; // next alien will be offset horizontally in the row
    }
    curx = fxpos; // reset horizontal position for aliens as we startign a new row
    cury = cury + invheight; // increae the vertical offset as we are starting a new row
}
My step event:


Code:
    for (i = 0; i < arraysize - 1; i++) //// Loop through our array,
        {
           
            show_debug_message("Iterations : " + string(arraysize -1)) 
            show_debug_message("Iteration c: " + string(i)) 
            show_debug_message( "AlienID: " + string(fidArray[i]))     //and print out each instance id 
            show_debug_message("x: " + string(x) + "    " + "y: " + string(y))     // print instance co ords
               
           
            with fidArray[i]
            {
                //do something with each instance
                hasMoved = 1;
            }
        }
        show_debug_message("DONE ITERATING THROUGH ALL ALIENS")
My debug output:

Iterations : 5
Iteration c: 0
AlienID: 100004
x: 0 y: -32
Iterations : 5
Iteration c: 1
AlienID: 100007
x: 0 y: -32
Iterations : 5
Iteration c: 2
AlienID: 0
x: 0 y: -32
Iterations : 5
Iteration c: 3
AlienID: 0
x: 0 y: -32
Iterations : 5
Iteration c: 4
AlienID: 0
x: 0 y: -32
DONE ITERATING THROUGH ALL ALIENS
 

samspade

Member
Basic issue is pretty straightforward. You have created an array of the correct size, but you are only populating the first 5 rows over and over and over again with:

Code:
fidArray[i]=instance_create_depth(curx, cury, 0, objInvader);
So ever iteration for the column just overwrights the id stored in fidArray until fidArray increases. The correct calculation should be:


Code:
fidArray[j+ (i * fcols)]=instance_create_depth(curx, cury, 0, objInvader);
I think. To test it I would do this:

Code:
var _pos = j+ (i * fcols);
show_debug_message("position in array " + string(_pos));
fidArray[_pos]=instance_create_depth(curx, cury, 0, objInvader);
 
Top