Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Bug [NetCode] RPC is created anywhere between 1-3 times on the server

Discussion in 'NetCode for ECS' started by wilbil741, Sep 26, 2020.

  1. wilbil741

    wilbil741

    Joined:
    Jun 17, 2018
    Posts:
    10
    Hi,
    Upgrading to NetCode 0.4.0, I've encountered a problem where my "GoInGame" RPC is created randomly between 1-3 times on the server. This causes 1-3 player ghosts to be instantiated and singleton problems on the client.
    upload_2020-9-26_17-10-51.png

    For reference here is the client code:
    Code (CSharp):
    1.         Entities.WithNone<NetworkStreamInGame>().ForEach((Entity entity, ref NetworkIdComponent id) =>
    2.         {
    3.             Debug.Log("Sent RPC");
    4.  
    5.             ecb.AddComponent<NetworkStreamInGame>(entity);
    6.  
    7.             Entity requestEntity = ecb.CreateEntity();
    8.             ecb.AddComponent(requestEntity, new SendRpcCommandRequestComponent() { TargetConnection = entity });
    9.             ecb.AddComponent<ConnectToServerRpc>(requestEntity);
    10.         }).WithoutBurst().Run();
    And here is the server code:
    Code (CSharp):
    1.         Entities.WithNone<SendRpcCommandRequestComponent>().ForEach((Entity reqEnt, ref ConnectToServerRpc req, ref ReceiveRpcCommandRequestComponent reqSrc) =>
    2.         {
    3.             Debug.Log("Received RPC");
    4.  
    5.             ecb.AddComponent<NetworkStreamInGame>(reqSrc.SourceConnection);
    6.  
    7.             #region operation
    8.             var serverPrefabs = GetBuffer<GhostPrefabBuffer>(ghostPrefabCollection);
    9.  
    10.             Entity prefab = Entity.Null;
    11.             for (int i = 0; i < serverPrefabs.Length; i++)
    12.             {
    13.                 Entity p = serverPrefabs[i].Value;
    14.                 if (HasComponent<Player>(p))
    15.                 {
    16.                     prefab = p;
    17.                     break;
    18.                 }
    19.             }
    20.             var player = ecb.Instantiate(prefab);
    21.  
    22.             ecb.AddBuffer<PlayerInput>(player);
    23.             ecb.AddComponent(player, new GhostOwnerComponent() { NetworkId = GetComponent<NetworkIdComponent>(reqSrc.SourceConnection).Value });
    24.  
    25.             ecb.SetComponent(reqSrc.SourceConnection, new CommandTargetComponent { targetEntity = player });
    26.             #endregion
    27.  
    28.             ecb.DestroyEntity(reqEnt);
    29.         }).WithoutBurst().Run();
    Is this an actual bug or is it a problem on my side?
    Thank you