Search Unity

Question Rpc method with target "NotMe" or "NotOwner" called twice by host/server

Discussion in 'Multiplayer' started by CurlyNikolai, Feb 4, 2024.

  1. CurlyNikolai

    CurlyNikolai

    Joined:
    Aug 23, 2021
    Posts:
    1
    Hi, I'm playing around with Netcode for Gameobjects 1.8.0 for learning and I'm trying to create a chatbox to understand Rpc:s. I simply want to send a message from a player that is received by all others. I do not want the sender to receive the message. The relevant code, which is attached to the player prefab, looks as follows:

    Code (CSharp):
    1. public class Player : NetworkBehaviour
    2. {
    3.     public override void OnNetworkSpawn()
    4.     {
    5.         base.OnNetworkSpawn();
    6.         if (IsOwner)
    7.         {
    8.             Terminal.ChatMessageSubmitted += HandleChatMessageSubmit;
    9.         }
    10.     }
    11.  
    12.     private void HandleChatMessageSubmit(string msg)
    13.     {
    14.         NewMessageRpc(msg);
    15.     }
    16.  
    17.     [Rpc(SendTo.NotMe)]
    18.     public void NewMessageRpc(string msg)
    19.     {
    20.         Debug.Log(msg);
    21.     }
    22. }

    I am a bit unsure how the SendTo target in the Rpc method works. If I set it to "SendTo.NotMe" or "SendTo.NotOwner", and call the method from a client, it is received by all other clients once, but twice for the host. If I set it to "SendTo.Everyone", the message is received once by everyone, including the sender.

    Is there a way for me to achieve the behaviour I want, i.e. "Send a message that is received by everyone else once", with a simple Rpc method like above? Thank you for any help, really appreciate it!
     
    danielalindan likes this.
  2. PoopofDog

    PoopofDog

    Joined:
    Nov 3, 2017
    Posts:
    3
    Hi, did you solve the problem?
    I was facing the same problem and was able to solve it.
    I share my method because the same people may continue to visit in the future.

    The key is to set the destination to Everyone and run it only when the sender's ID is different from the receiver's ID.

    Code that happens problem
    (When the client calls, it is called twice on the host)

    Code (CSharp):
    1. .
    2. void Start()
    3. {
    4.     SendMessageRpc("Hello");
    5. }
    6.  
    7. [Rpc(SendTo.NotMe)].
    8. public void SendMessageRpc(string message)
    9. {
    10.    Debug.Log(message);
    11. }
    12.  


    Corrected code
    (do return when the sender's ID and receiver's ID are the same.)

    Code (CSharp):
    1. .
    2. void Start()
    3. {
    4.     SendMessageRpc("Hello", NetworkManager.LocalClientId);
    5. }
    6.  
    7. [Rpc(SendTo.Everyone)].
    8. public void SendMessageRpc(string message, ulong senderID)
    9. {
    10.     if(NetworkManager.LocalClientId == senderID) return;
    11.    
    12.     Debug.Log(message);
    13. }
    14.  
    The ID of the sender or receiver
    NetworkManager.LocalClientId or NetworkManager.Singleton.LocalClientId.
     
    JohnnyConnor likes this.