Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NetCodeForGameobjects Rpc for a specific client

Discussion in 'Scripting' started by PersianKiller, Apr 20, 2022.

  1. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    I'm using netcode for gameobjects for my multiplayer game,I use this code to spawn an object in clients


    1. Code (CSharp):
      1.   [ClientRpc]
      2. public void test1ClientRpc()
      3. {
      4.      GameObject ob = Instantiate(Obj,new Vector3(0,0,0),Quaternion.Euler(0,0,0));
      5. }
    but the problem is that it sends this code to all clients, I want it to send it for a specific client.

    now I'm using this code,
    Code (CSharp):
    1.  
    2.      [ClientRpc]
    3.       public void test1ClientRpc()
    4. {
    5.      if (NetId==2)
    6.      {
    7.          GameObject ob = Instantiate(Obj, new Vector3(0, 0, 0), Quaternion.Euler(0, 0, 0));
    8.      }
    9. }


    it works fine but it has a problem .server still sends the code to all clients,then they check the condition and .... , I want to send it only for that specific client :)
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Seems like the same question someone else had in this thread. Which is also the appropriate forum section for questions about Netcode for GameObjects.

    Apparently you can use the rpc params to include an array of targets. At the bottom they also suggest that you'd cache it.
    So if you know for certain it would be single target only you could cache a ulong[] with just 1 entry. Reuse it by setting that entry just before you do the ClientRpc. Better than a new ulong[] every time you need to do such call.
    https://docs-multiplayer.unity3d.co...d-topics/message-system/rpc-params/index.html
     
    PersianKiller likes this.
  3. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114

    I read it but I couldn't find out anything ,can you show me an example please?(I know I shouldnt ask for this but please write the code for me,just a simple example)
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Yeah the documentation doesn't straight forward giving you a code example for this. I'm not all too familiar with everything yet either.

    As the documentation mentions
    The ClientRpcSendParams are part of the ClientRpcParams that you see in the documentation.
    Code (CSharp):
    1. [ClientRpc]
    2. void XyzwClientRpc(int framekey, ClientRpcParams clientRpcParams = default) { /* ... */ }
    ClientRpcParams
    has a
    Send
    field which is of type
    ClientRpcSendParams
    .

    So I'm trying to fiddle something together (untested). I'd say it should be something like this.

    Code (CSharp):
    1. using Unity.Netcode;
    2. using UnityEngine;
    3.  
    4. public class ExampleClass : NetworkBehaviour
    5. {
    6.     private ulong[] singleTarget = new ulong[1];
    7.  
    8.     [ServerRpc]
    9.     public void ExampleMethodServerRpc(ulong target, int number)
    10.     {
    11.         singleTarget[0] = target;
    12.         ClientRpcParams rpcParams = default;
    13.         rpcParams.Send.TargetClientIds = singleTarget;
    14.         ExampleMethodClientRpc(number, rpcParams);
    15.     }
    16.  
    17.     [ClientRpc]
    18.     public void ExampleMethodClientRpc(int number, ClientRpcParams rpcParams = default)
    19.     {
    20.         Debug.Log($"Received {number}", this);  
    21.     }
    22. }
     
    Last edited: Apr 20, 2022
    PersianKiller likes this.
  5. myazuid

    myazuid

    Joined:
    Jan 29, 2019
    Posts:
    26
    here's an example of how I've written it in my game

    Definition of the RPC:

    Code (CSharp):
    1. [ClientRpc]
    2.     public void NotifyHealthChangedClientRpc(int takeAwayPoint, ClientRpcParams clientRpcParams = default)
    3.     {
    4.         if (IsOwner) return;
    5.         Logger.Instance.LogInfo($"You got hit with the sword for {takeAwayPoint} damage");
    6.     }
    And how it is called:
    Code (CSharp):
    1. NotifyHealthChangedClientRpc(100, new ClientRpcParams
    2.                 {
    3.                     Send = new ClientRpcSendParams
    4.                     {
    5.                         TargetClientIds = new ulong[] { clientId }
    6.                     }
    7.                 });
    to get the clientID, you can just call gameObject.OwnerClientId on whatever object you have on the client in question