Back To Amethyst.rs

[Legion 0.4.0] Synchronizing entities between server and client

Hey there,

I’m starting to build a network multi-player game with Legion. I want to synchronize changes in entities on the server with their counterparts in connected clients.

I don’t know how to uniquely identify an entity in a way that is consistent across server and client and allows for random access to the entity.

I see that entities have an opaque ID, and I also see that the Canon produces a serialized “name” for entities, but I’m not sure if I am supposed to rely on these constructs to synchronize entity changes over time.

I thought to add a UniqueID(value) component to my entities, but I couldn’t figure out a way to access an entity by component value without iterating over all entities (and with hindsight, perhaps it doesn’t make sense to do that anyway).

What approach would you recommend for this kind of scenario?

Kind regards,

Chris

2 Likes

I was asking myself the exact same question this morning!
A somewhat easy and cheap way of doing it is to have a Uuid component on entities and a Hashmap<Uuid, Entity> resource. This way you can map entities to uuid using iteration (what most servers will be doing) and uuid to entities (what clients will be mostly doing when receiving data).

If you have a better idea, let me know! :slight_smile:

2 Likes

Thank you for the response!

What I tried (and it seems to work) is to cross-associate entities by the “name” generated by Canon. (I think) this works for me because all entity creation is currently done on the server by serializing the world with all entities containing a NewlySpawned component and deserializing that view of the world into the world on the client, so there are instances of Canon on both sides that have a common set of canonized names that correlate with local entity IDs.

Is it okay to use Canon this way? I’m worried that it is either more brittle than I think, or that I’m missing something important where this strategy will fall apart.