ID Generation question

Hi guys!

I have a question about ID generation for entities. For example, I generated 500 IDs and then i removed 50 IDs (from 100 to 150 (numbers)). How can i fill removed numbers with new IDs?
I mean i don’t want to create ID501, ID502, ID503…
I want to fill removed numbers ID100, ID101, 102…

Does anybody have an example how to check for empty IDs in the list? Thank you.

Put all removed IDs in a list, and then just poll that list when you need a new ID (or create a new if the list is empty),

use an array as a LIFO-queue would be most efficient I think.

Maybe check how Artemis does it. It’s optimized for being able to generate millions of entities per second.

EnityManager: https://code.google.com/p/artemis-framework/source/browse/src/com/artemis/EntityManager.java?name=default&r=44dc9d7b3969e2c6a76f4344cda638f7a5d3f404
Bag: https://code.google.com/p/artemis-framework/source/browse/src/com/artemis/utils/Bag.java?name=default&r=44dc9d7b3969e2c6a76f4344cda638f7a5d3f404

Artemis is using it’s own ArrayList (called Bag) and uses it as a stack/LIFO-queue.

Thank you guys!

I suggest never reusing entity IDs. Theres no reason to do so and it can only cause confusion.
The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless there’s emotes that hint otherwise or there’s an increased use of exclamation marks and all-capital words.

2 Likes
@loopies said: Maybe check how Artemis does it. It's optimized for being able to generate millions of entities per second.

EnityManager: https://code.google.com/p/artemis-framework/source/browse/src/com/artemis/EntityManager.java?name=default&r=44dc9d7b3969e2c6a76f4344cda638f7a5d3f404
Bag: https://code.google.com/p/artemis-framework/source/browse/src/com/artemis/utils/Bag.java?name=default&r=44dc9d7b3969e2c6a76f4344cda638f7a5d3f404

Artemis uses a combination of UUID and a second ( simple ) incrementing and reusable integer for user friendly ID’s.

@normen said: I suggest *never* reusing entity IDs. Theres no reason to do so and it can only cause confusion. The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless there's emotes that hint otherwise or there's an increased use of exclamation marks and all-capital words.

I definitely agree with this. There is no reason to reuse IDs. Use a long and you will never wrap, ever.

To reuse IDs you only make everything slower and cause potential problems if something holds onto an ID longer than you expect. Just don’t do it.

1 Like

Thank you all. I’ll not reuse IDs.