Game object's unique ID

I have a problem occurring from time to time when Defold assigns ID to an object created with factory that is equal to another game object’s ID which is created with different factory. This issue occurs almost regularly when two objects are created (with the same factory) in short time interval. Is there a way to prevent such issue and how?

Do you cache the ids somewhere? Defold reuses old ids of destroyed objects for new ones.
Basically when you delete a game object, you have to clear any references to it in your code.

2 Likes

Thanks for response Sergey. Yes I do cache IDs . The problem is that sometimes Defold reuses ID of an existing object and I don’t know if this is expected behaviour or a bug. I’m using Defold Editor v1.2.157.

I am using a lot of factories in my projects and didn’t find this issue.
If it really sometimes uses same ids for two objects, then it’s a bug. If you can create a test project that demonstrates the problem, that would be helpful for the team.

3 Likes

The ID’s are reused after the object has been deleted, as Sergey says.
Note that two collections’ objects may have the same ID’s (e.e “instance0”), since the full path is still unique.

Where do you cache theseID’s? Do you store multiple collections’ ID’s there?

2 Likes

Thanks for feedback. I’ll refactor the code and use object’s full path instead of ID and see if the issue persists. I thought that IDs of objects created using factory are unique within a collection.

Try to work with table object like {id=“instance…id”, is_dead=false, …} instead of pure instance ID in your code and always check is_dead before do any things with object. When delete instance just turn this trigger to true. So, if somewhere in code, especially in delayed method or closure, you find an old object - checking is_dead will prevents an error.

2 Likes

They are unique within a collection.
They may be reused though, which you need to take into consideration.

If you think it’s a bug, please give us a repro case project to test with.

2 Likes