• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

Need help changing variables through a "with" statement


Basically I'm trying to create three instances of an object called oShopItems. I'm using a "with" statement to try and externally change the positions of each of these three instances once they are created. The code (using a persistent meta obj) looks like this:

var shop1 = collision_rectangle(280, 200, 318, 220, oShopItems, true, true)
if shop1 = noone && global.storeClosed = false && (!shopItems) {
var Obj1 = room_instance_add(rStuffPlaceInterior, 300, 211, oShopItems);
with (Obj1)
        DrawHereX = 300
        DrawHereY = 211
    shopItems = true;
Within oShopItems "create" event, the code looks like this:

depth = 100;

shop_item[0] = sBookcase_01
shop_item[1] = sCoffee_Table_01
shop_item[2] = sRug_01
shop_item[3] = sRug_02
shop_item[4] = sCake_01
shop_item[5] = sChair_01
shop_item[6] = sLamp_01
shop_item[7] = sLamp_02
shop_item[8] = sHaunted_Doll_01
shop_item[9] = sTV_01
shop_item[10] = sConspiracy_Board

random_object = shop_item[irandom_range(0,10)]

DrawHereX = 0;
DrawHereY = 0;
And then oShopItems also has a "draw" event which looks like this:

draw_sprite(random_object, 0, DrawHereX, DrawHereY);
When I change the DrawHereX and DrawHereY values inside the oShopItems "create" event, it works fine. But I want to change it within "var (Obj1)" from within the meta object. Nothing happens when I do this though.
Any help would be greatly appreciated!


🍋 *lemon noises*
GMC Elder
vars are temporary variables, so they stop existing when the event ends. This means you can't reuse a value from a previous step (e.g. the index returned by instance_create).

Also, you're looking for instance_create_layer, room_instance_create is meant to be used to modify a different room from the one you're in and doesn't return an instance index (because the instance doesn't EXIST until you go to that room)


Not sure about this function room_instance_add.
In the manual, there is a description to this "With this function you can add an instance into any room other than the current one". I think that you are not getting correct (or at all) instance_id.

Dont know what version you have but you can try:

instance_create_layer(x, y, layer_id, obj);


instance_create(x, y, obj);
(if you are using gms1.4)