How to uniquely identify an instance of an object even when it's reloaded


If I have multiple instances of a scene in the SceneTree, I want to be able to uniquely identify which is which, in a way that is consistent even when the game is restarted or the scene is reloaded.


For example, say my game has a player you can move around, and two rooms. There is a door that allows you to go between the rooms. The rooms are different scenes, when you go to the other room, it is loaded into memory and the previous one is freed.

In each room is two treasure chests, and I need to remember which ones the player has opened already, so that when they go back to a room, I can render the previously opened chests as open.

How can I store which treasure chests are already opened?

My current solution is to export a variable on the treasure chest representing a unique id, and typing something random in that field on the editor. In this way, I can save reference to the specific instance of the chest that should be open or not in a Singleton script. But I’m wondering if there’s built-in way to do this so I don’t need to type anything manually for the id.

you could use a combination of :
1 - the scene filepath (will be unique for each scene)
2 - the object path in that scene (will be unique for each object in the scene)

1 Like

You can see a related post here :

1 Like

Thank you guys!
I was missing the fact that you can reference a node uniquely in the scene by its path with node.get_path()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.