Zay-es: How safe is it to use EntityID.getId() for storing things in maps?


Hi everyone,

when I put things on the scene graph from EntityData with zay-es, I do it like this:

  • I create a HashMap with some identifier (like a string, a uuid, or a long) and a Spatial.
  • I use a simple loop with an EntitySet and use applyChanges() and getAddedEntites().
  • I determine what model I should use for the data I find in the added entities.
  • I add that model to the map.
  • When adding it to the map, I also add it to a Node.

I have considered using the Node’s internal getChild(String) to look up values, but I’d get some issues when adding two children with the same name (in that case, both are on the scene, but only one will return from that getter).

Mostly, I have used either Longs or UUIDs to make each Entity unique, and be able to find their spatial back in a map, and stored these values in EntityComponents.

However, I was wondering whether I could use the Entity’s id itself as an identifier for such a map.
It should not change during the lifetime of an entity, and a Long is long enough (I don’t necessarily need a UUID, which is based on two longs I believe).


Yes, just use the EntityId objects as key for your HashMap entries. :slight_smile:


Yes, EntityId is safe for maps… it’s kind of its entire job and all of Zay-ES would break if that weren’t true.

You can also see that I do exactly this in “insert every Zay-ES example I’ve ever posted”. So you can look at that for good examples.

The most recent Sim-ethereal examples will even show using some of the SiO2 convenience classes. The important one in this conversation is EntityContainer which you can extend to easily handle these sorts of entity to Spatial mappings.


EntityContainer (can just be cut-pasted if you don’t want the whole SiO2 library… but it’s small):

Working networked game example using Zay-ES but the non-networked parts are still useful:

Specifically, ModelViewState:

The simplest example of mapping an entity to a spatial using EntityContainer:


Thanks for pointing that out! I have seend SiO2 before, but I never really looked into it.
The EntityContainer seems just what I need!