Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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

Discussion in 'Entity Component System and C# Job system' 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:
    287
    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:
    655
    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.