#Multiplayer: Entity Id Sync

4 messages · Page 1 of 1 (latest)

teal wedge
#

hi, i want to make Multiplayer System for a 3d Sandbox Voxel Game (repo)

to represent an Entity Identifier for Server & Clients, should i generate id in server via commands.spawn().id() and lookup in client via commands.get_or_spawn(id_from_server)? does this way dangerous/inappropriate? what is best way? (is maintain a Map<IdFromServer, Entity> and increase a map lookup everytime server sent a entity-update packet?)

detailed:

I want the server and clients keep 'Entity Data' in sync.

e.g. when Server have a Pig entity, then the server should broadcast a PacketNewEntity{ entity_id: u32, type: enum }, PacketEntityPos {entity_id: u32, pos: Vec3 } to all clients.

what is the best practice about the entity: u32?

here is my 2 ways:
the entity_id: u32 is came from server commands.spawn().id() or whatever, and lookup in client by

  1. commands.get_or_spawn(entity_id).
    but this may dangerous, cuz the entity_id sent from server may already been created/occupied in client.

  2. client maintain a Map<EntityIdFromServer, Entity> and lookup the map everytime server sent an operation for an entity.
    which eliminated the 'entity_id already occupied in client' issue, but increases a extra map lookup.

humble marten
#

The approach i took was 2 bc to me i would rather make the code clean and easy to understand and then optimize later if necessary. The extra lookup shouldnt cause a significant impact on performance. Option 1 shouldnt really ever have a collision but actually 32 bits is so small it very well might .. if it was 256 bits then opt 1 would be more viable

#

Also to simplify my code more, i used a component called Unit and generated my own uuid which i shared from server to client to do state sync. This way i kept the concept of an entity local only and i did use a lookup mapping tho. Also check out the bevy net crates there are good ones

teal wedge