I've built my own entity manager, as I found out some days ago, it is very similar to the Artemis framework.

For now, I just use integers as entity ids. Behind the scenes, there is a simple integer counting up every time a new entity is created so that the ids are handed out in incrementing order by the manager.

But recently I faced the problem that I cannot load multiple savegame files into the same scene. The issue here is that two savegames could use the same entity id for different things, causing a conflict when being load into the same scene.

Anyhow, the ability to load multiple savegames is useful to keep separated player data (his skills, inventory, custom items and tools he crafted) with scene data (rocks, trees, players in a town) and data from mods (cool new weapon pack).

So I need unique ids among every savegame file that will be created at all, without them knowing about each other. The idea that came to my mind is using timestamps as entity ids instead of consecutive integers.

(Since multiple entities might be created in the same millisecond, the timestamp should contain microseconds. And to play safe there could be also a mechanism to suffix a consecutive number if multiple entities are created in the same microsecond. To be honest I don't know how fast modern CPUs are and if they could do that, but I guess they can.)

Since I haven't heard of other people doing that, I assume that there is a trade-off. What are the disadvantages of using timestamps as entity ids? I think the performance hit will be minor and be can neglect that point.

  • 4
    \$\begingroup\$ It sounds like you're trying to re-invent a GUID? \$\endgroup\$ – Oskar Duveborn Aug 12 '13 at 10:21
  • \$\begingroup\$ @OskarDuveborn Pointing that out was really helpful. I didn't know that such concept exist. \$\endgroup\$ – danijar Aug 12 '13 at 11:30

Timestamps are a terrible thing to use for IDs, even with a high-precision timer 2 entities do not necessarily have to have a different ID (think how little time can have passed between 2 calls to CreateEntity()). Also you cant fit time into a simple 32bit integer. If you're going to load entities from two different sources there is a much easier fix. Load the first set of entities, find the highest ID and add this number to all IDs in the second set while loading.

Also, since you're talking savegames I'm not sure how ever 2 entities can have the same ID even though they are saved in multiple files. If they contain a saved state from the same game then all IDs should be unique.

Other scenarios you mention, like weapon packs and stuff should be wired by factory methods, which assign each component from the weapon pack fresh IDs while loaded. Wiring everything together instead of relying on predefined IDs.

  • \$\begingroup\$ I think handing out the same id can be avoided by adding a suffix number whenever the multiple entities are created within the same microsecond. What do you think? Anyway, your further thoughts help me a lot. I already thought about the concept, that mods add their new stuff by scripting methods. But when a modder creates a new village for the game world, I would be pain to write a creation script for all the objects. To generate it feels hacky to me, but I might be wrong. How would you handle that case? \$\endgroup\$ – danijar Aug 12 '13 at 9:41
  • \$\begingroup\$ Adding a suffix adds needless extra complexity I think, and the strategy required to fix all those suffixes in save games is the same as when using normal ids so you don't gain much :). As for modders creating a village. I'm pretty sure its a lot less hacky to import a script with statements like 'CreateVillager()' then directly importing their save-state. :) \$\endgroup\$ – Roy T. Aug 13 '13 at 6:09
  • \$\begingroup\$ The user Oskar Duveborn pointed me to GUID, there is a nice implementation in the Qt framework which creates globally unique ids from system time, clock cycles and MAC network address. That would make it possible to import multiple savegames in the same scene, without breaking scripts that refer to a given id. So global ids are an option now. But as you stated, it may not be very useful. But do you think I should force modders to create their new parts of the world by scripting? I think it is very counter intuitive to type in coordinates by value. \$\endgroup\$ – danijar Aug 16 '13 at 12:30
  • \$\begingroup\$ GUIDs could work, but there is always the danger of collision. I don't think modders really need scripting but under-the-hood you could create a script to build the building instead of just serializing it. \$\endgroup\$ – Roy T. Aug 19 '13 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.