SOLVED Assigning IDs to instancing for recall.

Seryal

Member
I'm trying to assign some IDs to instances to refer to them later. Somethings not right and I may just be tired. I'll try to post just the relevant code only. I don't know why I struggle so much with assigning IDs to instances.

In the Object: "Menu" Create:
GML:
newgame = instance_create_layer(x-hb, y-vb, "Instances", oBorder);
newgame = id;

exitgame = instance_create_layer(x-hb, y+pad-vb, "Instances", oBorder);
exitgame = id;
In the Object: "Bullet" Collision Event with Object: "Border":
GML:
with (other) {
    if (id == newgame) {
        id.dmg = id.dmg + 15;
    }

    if (id == exitgame) {
        id.dmg = id.dmg + 10;
    }
}
instance_destroy();
In the Object: "Border" Step:
GML:
if (dmg >= 250) && id == newgame {
    startGame();
    instance_create_layer(x, y, "Instances", oCountdown);
    with (oMenu) instance_destroy();
    with (oBorder) instance_destroy();
} else if (dmg >= 250 && id == exitgame) {
    game_end();
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
GML:
newgame = instance_create_layer(x-hb, y-vb, "Instances", oBorder);
newgame = id;

exitgame = instance_create_layer(x-hb, y+pad-vb, "Instances", oBorder);
exitgame = id;

You are not assigning IDs to instances here. You're creating an instance, storing its ID in a variable and then overwriting the value of that variable with the ID of the instance that just created the other instance. The instance that created the other instance now has a variable that holds its own ID.

newgame and exitgame both hold the ID of an instance of Menu.

If you want to assign a variable to an instance you just created, you do it like this:
GML:
inst = instance_create...;
inst.variable = value;
 

Seryal

Member
So by using this code instead...

GML:
var _newgame = instance_create_layer(x-hb, y-vb, "Instances", oBorder);
_newgame.ngid = id;

var _exitgame = instance_create_layer(x-hb, y+pad-vb, "Instances", oBorder);
_exitgame.egid = id;
...in the Border object, can I not use a check like "if (id == ngid) startGame();" ? I've changed some variables, still no errors, but not functioning as intended.

EDIT: Also, ngid is showing an ID of 1000003 while the egid is showing undefined and they are virtually the same code when assigning IDs that way.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
...in the Border object, can I not use a check like "if (id == ngid) startGame();" ? I've changed some variables, still no errors, but not functioning as intended.
id will be the ID of an instance of Border but ngid will be the ID of an instance of Menu. These will never be equal.

Sanity check: Are you actually trying to assign the creator's ID to an instance it just created (what you are doing now), or are you trying to assign a distinct value to every instance created? The rest of your code makes me think you're trying to add a way of distinguishing created instances, and assigning the creator's ID will not have that effect at all (all of them will have the same value).
 

Seryal

Member
No I'm not trying to refer to the ID of the Menu at all. I'm just really confused. I just want the Menu to spawn 2+ instances of the same Border object that I can check in an if statement, so I can make changes to a specific Border instance and execute what the button is supposed to be doing.


I'm not sure why ngid would be an ID of the Menu, when it appears like an attribute of the created Border instance? Do ngid and id need to be swapped? I'm sorry I'm just not understanding.

EDIT: It was all functioning fine when I used 2 multiple object but I wanted less code with the more buttons/borders I create in the future.
 

Seryal

Member
Okay. I'm just stupid, making it way more complicated on myself, I figured it out. Thank you for all your help, truly!
 
Top