Search Unity

How to start a method through Unity Transport Preview / looking for a simple NetCode RPC example

Discussion in 'Unity Transport' started by JanneSilt, Dec 9, 2020.

  1. JanneSilt

    JanneSilt

    Joined:
    Jun 21, 2018
    Posts:
    5
    My game is separated in Server and Client versions and I have succesfully established a connection between these two Unity instances. Now how can the server version of the game start a method in the Client? Let's say on Server I have GameManager.cs where to start a method (in client) InstantiateCards() in ClientManager.cs file which is in Cube-named gameobject. Any example maybe? Thank you?
     
  2. JanneSilt

    JanneSilt

    Joined:
    Jun 21, 2018
    Posts:
    5
    or can anyone provide me with help understanding a simple RPC calls of NetCode? All I'm looking for is a simple RPC on the server side to call a function on the client. The docs seem to only provide more complex ones, listening for input if I understand it right...
     
  3. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    130
    Here is a server system that detects when a player disconnects, and the broadcasts an RPC to all clients to notify. them.

    Code (CSharp):
    1.  public class ServerDetectDisconnectSystem : ComponentSystem {
    2.         protected override void OnUpdate() {
    3.  
    4.             Entities
    5.                 .ForEach((ref NetworkIdComponent networkIdComponent, ref NetworkStreamDisconnected networkStreamDisconnected, ref PlayerID playerID) => {
    6.                     DebugUtilities.ServerLog(
    7.                         $"Disconnect detected, netID:{networkIdComponent.Value} playerID: {playerID.Value}, Time: {Time.ElapsedTime} ");
    8.  
    9.                     NativeArray<Entity> networkConnections = EntityManager.CreateEntityQuery(typeof(NetworkIdComponent)).ToEntityArray(Allocator.TempJob);
    10.                     if (networkConnections.Length <= 1) {
    11.                         DebugUtilities.ServerLog($"No one to broadcast disconnect to.");
    12.                     }
    13.                     else {
    14.                         BroadCastLeftConnection(playerID.Value, networkIdComponent.Value);
    15.                     }
    16.                     networkConnections.Dispose();
    17.                    
    18.                    
    19.                 });
    20.         }
    21.        
    22.  
    23.         void BroadCastLeftConnection(FixedString512 playerID, int networkID) {
    24.  
    25.             var connectionBroadcastEntity = PostUpdateCommands.CreateEntity();
    26.            
    27.             PlayerLeftCommand playerLeftCommand = new PlayerLeftCommand
    28.             {
    29.                 PlayerNetworkID = networkID,
    30.                 PlayerID = playerID
    31.             };
    32.            
    33.             PostUpdateCommands.AddComponent(connectionBroadcastEntity, playerLeftCommand);
    34.             PostUpdateCommands.AddComponent(connectionBroadcastEntity, new SendRpcCommandRequestComponent());
    35.         }
    36.     }

    Here's the actual RPC command object

    Code (CSharp):
    1.    public struct PlayerLeftCommand : IRpcCommand {
    2.         public int PlayerNetworkID;
    3.         public FixedString512 PlayerID;
    4.     }

    Here is the client side code that receives the RPC and then fires an event to the the rest of my program.

    Code (CSharp):
    1. [UpdateInGroup(typeof(ClientSimulationSystemGroup))]
    2.     public class ClientReceivePlayerLeftBroadcastSystem : ComponentSystem {
    3.         protected override void OnUpdate() {
    4.            
    5.             Entities.ForEach((Entity entity, ref PlayerLeftCommand playerLeftCommand,
    6.                 ref ReceiveRpcCommandRequestComponent req) => {
    7.                 PostUpdateCommands.DestroyEntity(entity);
    8.  
    9.                 NetworkIdComponent networkIdComponent = GetSingleton<NetworkIdComponent>();
    10.                 bool isThisClient = playerLeftCommand.PlayerNetworkID == networkIdComponent.Value;
    11.  
    12.                 DebugUtilities.ClientLog($"Server notifying that {playerLeftCommand.PlayerID} Left. NetID :{playerLeftCommand.PlayerNetworkID} is this self?: {isThisClient}");
    13.  
    14.                 if (!isThisClient) ConversationEvents.PlayerLeft(playerLeftCommand.PlayerID.ToString());
    15.  
    16.             });
    17.         }
    18.     }
     
unityunity