Search Unity

Is it possible to assign entity value directly in a networked game to sync server and client?

Discussion in 'Data Oriented Technology Stack' started by PixelMind, Dec 5, 2018.

  1. PixelMind

    PixelMind

    Joined:
    Aug 16, 2013
    Posts:
    100
    I’m building a item / inventory heavy networked game and I’m currently investigating using hybrid ECS. In theory it seems like a good design for networked game.

    To my understanding ECS’s entity is simply an id number under the hood. So this got me thinking if it would be possible to directly use it as an address to a specific entity on both client and server. So essentially “entity 123456” on server would directly map to “entity 123456” on client. And therefore sending a simple “state of entity 123456 is this” -style messaging would be rather straightforward to implement.

    However I’m not experienced with the ECS at all yet. Is this viable strategy? Is it possible to directly assign an entity id to an entity. Or should I have some hash / directory style bookkeeping layer between server and client so I can refer an entity directly?

    TLDL: Can I directly assign entity id number so I can sync messages on client and server?
    Does the api have something like EntityManager.GetOrCreateEntity(123456) somewhere?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    485
    theoretically, if your world is completely symmetric and deterministic, the entity ID should be the same for all clients, so you could send that directly.
    but if you have even one different entity (e.g client has rendering while server doesn't, or clients in different position need different representation) then you are screwed.

    you can look at FPS sample for how they do it (from what I understand, they have a known set of entities to sync, and they have a ReplicatedEntity component with an unique id, then sync entities using that id (you can use a NativeHashMap<int,Entity> for lookup)
     
  3. PixelMind

    PixelMind

    Joined:
    Aug 16, 2013
    Posts:
    100
    Thanks @M_R
    In my case I definitely can't rely on determistic IDs as my server will constantly create and destroy entities. And players can join the game at any point. But I was thinking that server just sends all its entities on connect and syncs their state. And then on client assign the entity value (not sure if this is possible) and set the state according to server's state.

    But having a some sort of book keeping layer might make more sense as I could optimize the packet size a bit instead of sending a full integer or whatever ECS uses as an entity id internally.

    I'll look into the FPS sample and meditate this a bit more.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,522
    It’s not just ID, it’s ID+Version, and entity with same ID, but different Version - are different.
     
  5. PixelMind

    PixelMind

    Joined:
    Aug 16, 2013
    Posts:
    100
    Oh I see. Thanks @eizenhorn.
    Overall it sounds like using entity directly doesn't make sense for what I initially had in mind.