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
-
commands.get_or_spawn(entity_id).
but this may dangerous, cuz the entity_id sent from server may already been created/occupied in client. -
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.