Search Unity

Client Server Entity Sync

Discussion in 'Entity Component System' started by l33t_P4j33t, May 13, 2020.

  1. l33t_P4j33t

    l33t_P4j33t

    Joined:
    Jul 29, 2019
    Posts:
    232
    Are you able to fetch entities by just constructing a new entity with the corresponding index and version? and more importantly, will an entity have the same version and index on the server and on every client?

    looking at the entity debugger, it surprisingly seems to be so, but im not sure if it'll always be the case or if its just a coincidence

    would something like this work?

    Code (CSharp):
    1. [UpdateInGroup(typeof(ServerSimulationSystemGroup))]
    2. class DestroyEntitySystem : ComponentSystem {
    3.     protected override void OnUpdate() {
    4.         Entity toDestroy = GetSingleton<Tag_ToDestoy>();
    5.                
    6.         Entities
    7.             .WithAll<NetworkIdComponent>()
    8.             .ForEach((Entity connectionEnt) => {
    9.                 Entity rpcEntity = PostUpdateCommands.CreateEntity();
    10.                 DestroyEntityRPC destroyEntityRpc = new DestroyEntityRPC{index = toDestroy.Index, version = toDestroy.Version};
    11.                 PostUpdateCommands.SetComponent(rpcEntity, destroyEntityRpc);
    12.                 PostUpdateCommands.AddComponent(rpcEntity, new SendRpcCommandRequestComponent { TargetConnection = connectionEnt });
    13.             });
    14.     }
    15. }
    16.  
    17. [UpdateInGroup(typeof(ClientSimulationSystemGroup))]
    18. class ProcessDestroyRPC : ComponentSystem {
    19.     protected override void OnUpdate() {
    20.         Entities
    21.             .ForEach((Entity reqEnt, ref DestroyEntityRPC rpc, ref ReceiveRpcCommandRequestComponent reqSrc) => {
    22.            
    23.             Entity toDestroy = new Entity{Version = rpc.version, Index = rpc.index};
    24.             PostUpdateCommands.DestroyEntity(toDestroy);
    25.             PostUpdateCommands.DestroyEntity(reqEnt);
    26.         });
    27.     }
    28. }
    29.  
    30. class DestroyEntityRPC : IRpcCommand {
    31. ...
    32. }
     
    Last edited: May 13, 2020
  2. l33t_P4j33t

    l33t_P4j33t

    Joined:
    Jul 29, 2019
    Posts:
    232
    i think the pattern is its the same only for non runtime generated entities.
    the best way i can think of referencing specific entities is by comparing their translation

    how does livelink solve this problem? how can they tell which entity was changed in the editor
     
    Last edited: May 13, 2020
  3. Wobbers

    Wobbers

    Joined:
    Dec 31, 2017
    Posts:
    55
    You can not rely Version/Index.
    In Netcode, all Entities that are synced between client/server have the GhostComponent, which stores an Id you can use to identify the entities.
    So you would have to send that Id in the Rpc and then check which entity has a GhostComponent with that Id and destroy that.