SOLVED Dynamically change image_index

enique

Member
Using the code below I populate the room with an object, but I'm also trying to dynamically change the image_index of the object that gets placed down. For example the first object gets placed as image_index 1, then the next object that gets placed would be image_index 2, etc.

GML:
for (var i = 0; i < room_width div TILE_SIZE; i++)
{
    for (var j = 0; j < room_height div TILE_SIZE; j++)
    {
        instance_create_layer(i * TILE_SIZE, j * TILE_SIZE, "Walls_Instances", obj_block)
        obj_block.image_index += 1;
    }
}
I've also tried a few other variations of this with no luck. Maybe I need to some how identify each instance of that object. The code you see just places all instances as image_index 2. Any assistance would be appreciated. Thank you.

EDIT: Btw, IDE v2.3.3.574 and Runtime v2.3.3.437
 
D

Deleted member 13992

Guest
As specified in the manual, instance_create_layer returns an instance id, so you can just do it this way.

Though you will probably need to keep track of how much you are adding to image_index, since it'll just default to 0 with each newly created instance.

GML:
var inst = instance_create_layer(i * TILE_SIZE, j * TILE_SIZE, "Walls_Instances", obj_block)
inst.image_index = count_up;
count_up++;
 

Nidoking

Member
No, you need to keep track of the image_index value you want in another variable and set it to that. The newly-created instance isn't going to know what that value is until you tell it.
 

TheouAegis

Member
with instance_create_layer(i * TILE_SIZE, j * TILE_SIZE, "Walls_Instances", obj_block)
image_index = i * (room_height div TILE_SIZE) + j;​
 

enique

Member
As specified in the manual, instance_create_layer returns an instance id, so you can just do it this way.

Though you will probably need to keep track of how much you are adding to image_index, since it'll just default to 0 with each newly created instance.

GML:
var inst = instance_create_layer(i * TILE_SIZE, j * TILE_SIZE, "Walls_Instances", obj_block)
inst.image_index = count_up;
count_up++;

Thank you. I was trying to use a variable, but wasn't including the instance create layer. I was just trying to do var inst = 0 and add using that. Thanks everyone for the replies though.
 
Top