• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 Is there a way to get the unique instance identifier? (not ID)

Hello,

I have a decent, but important problem when trying to communicate with unique instances in my game. I could use the ID of course (stored in variable id), but this is not static and changes if I add any nex instances to the layer. There is however this instance identifier thing, for example inst_214A6FAD which can be seen in the Instances Layer Properties. I searched whole Google but could not find an answer how to get this identifier in the code, isn't there some built-in variable where I can read this value from? I need this for applications like:
GML:
with (collision_rectangle(x, y, x + 10, y + 10, oWall, false, false)) {
    if ([SEARCHED_VARIABLE] == inst_214A6FAD) //do something...
}
So my question is: Is there a way to get the unique instance identifier of an instance with code?

Thanks for your help in advance!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
First, you have to tell us why you think you need this identifier? The instance ID should be enough... What are you trying to do?
 

Nidoking

Member
I could use the ID of course (stored in variable id), but this is not static and changes if I add any nex instances to the layer.
The id variable is static with respect to the execution of the game. If you have an instance in the game, and you need to know how to refer to it, use its id. If you want to refer to a specific instance that you created in the Room Editor using the hardcoded ID in the Room Editor, it's in the Room Editor. They're two different ways to refer to an instance, depending on when and how you created it, and therefore used for different purposes.

If you want to compare instance ids, have you considered (inst_214A6FAD).id?
 

FrostyCat

Member
inst_214A6FAD holds an instance ID the same way pi holds 3.141592653589793. Therefore, by looking everywhere else on Google, you sent yourself down a rabbit hole.

The one solution you discounted is the actual correct answer:
GML:
with (collision_rectangle(x, y, x + 10, y + 10, oWall, false, false)) {
    if (id == inst_214A6FAD) //do something...
}
Or even more simply:
GML:
with (collision_rectangle(x, y, x + 10, y + 10, inst_214A6FAD, false, false)) {
    //do something...
}
 

TheouAegis

Member
Instances created via instance_create* functions are assigned an id based on an incremented variable. Instances that are created in the IDE will always have the same id until the program is closed; it won't change unless you modify the project in the IDE.
 

CMAllen

Member
Instances created via instance_create* functions are assigned an id based on an incremented variable. Instances that are created in the IDE will always have the same id until the program is closed; it won't change unless you modify the project in the IDE.
Maybe that's what was the original problem -- referencing an object's pre-assigned ID from the IDE while still developing the project? The pre-assigned value keeps changing because it's still in development, so the user is unsure how to reference it in code. Of course, I'm not sure why the pre-assigned ID should be changing at all. Once it's dropped into a room, that value shouldn't change unless you delete the object in question and then add a new one to the project. Which, of course, would render the old ID invalid and generate a new one.

I think we're missing an important piece of information.
 

Roldy

Member
I think we're missing an important piece of information.
No. All the information has been provided in this thread.

An object's id may change but you can always reference it by its name. That is how you are supposed to reference it. You never need to know an objects id, that is for internal use for GMS.

if you have an object named 'o_myObject' then you just type 'o_myObject' in code to reference it. The id is mostly irrelevant for the user to directly reference it.

An instance's id is easily obtainable through the instance variable .id. But if you need to reference a specific one you can use its name. e.g.

1596772430411.png

GML:
myInstance = inst_556040;

if (myInstance.id == inst_556040) {
   // this will always be true.
}
If you don't like the weird number appended to the name then you can name it anything you want through the instance properties dialog.

1596772247029.png

e.g.

GML:
myInstance = i_just_renamed_this_instance;

if (myInstance.id == i_just_renamed_this_instance) {
   // this will always be true.
}
It all works very simply and straight forward.

For resources and objects and instances made in the IDE you reference them with their name. Their name does not change unless you change it.
 

CMAllen

Member
No. All the information has been provided in this thread.

An object's id may change but you can always reference it by its name. That is how you are supposed to reference it. You never need to know an objects id, that is for internal use for GMS.

if you have an object named 'o_myObject' then you just type 'o_myObject' in code to reference it. The id is mostly irrelevant for the user to directly reference it.

An instance's id is easily obtainable through the instance variable .id. But if you need to reference a specific one you can use its name. e.g.

View attachment 33207

GML:
myInstance = inst_556040;

if (myInstance.id == inst_556040) {
   // this will always be true.
}
If you don't like the weird number appended to the name then you can name it anything you want through the instance properties dialog.

View attachment 33206

e.g.

GML:
myInstance = i_just_renamed_this_instance;

if (myInstance.id == i_just_renamed_this_instance) {
   // this will always be true.
}
It all works very simply and straight forward.

For resources and objects and instances made in the IDE you reference them with their name. Their name does not change unless you change it.
I was going to respond, but after re-reading the original post, I think I may have misunderstood the inquiry. Carry on.
 
Top