Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

ClientRpc is called only on the current object, not all

Discussion in 'Connected Games' started by demonixis, Dec 19, 2017.

  1. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    156
    Hello,

    All players have a structure with some informations like name and faction. When a client is connected, it sends a command to the server with the name and the faction. The server update those values and call a RPC function to update all other objects (replicated players). The problem is that the RPC call is only made on the owning client.

    There is some code

    Code (CSharp):
    1. public enum PlayerTeam
    2. {
    3.     Team1, Team2
    4. }
    5.  
    6. public class PlayerStats
    7. {
    8.     public string Name;
    9.     public PlayerTeam Team;
    10.     public bool IsCapitain;
    11. }
    12.  
    13. public sealed class Player : NetworkBehaviour
    14. {
    15.     public static string EmulatedCommandLine = string.Empty;
    16.  
    17.     [Header("UI")]
    18.     [SerializeField]
    19.     private Text m_PlayerNameText = null;
    20.     [SerializeField]
    21.     private Text m_PlayerFactionText = null;
    22.  
    23.     private void Start()
    24.     {
    25.         if (isLocalPlayer)
    26.         {
    27.             SetEnabled<Camera>();
    28.             SetEnabled<FlareLayer>();
    29.             // Etc...
    30.  
    31.             var cmd = Environment.CommandLine;
    32.  
    33.             if (EmulatedCommandLine != string.Empty)
    34.                 cmd = EmulatedCommandLine;
    35.  
    36.             // Parsing commands
    37.             if (!string.IsNullOrEmpty(cmd))
    38.             {
    39.                 var commands = cmd.Split(' ');
    40.                 var property = string.Empty;
    41.                 var value = string.Empty;
    42.                 string[] tmp;
    43.  
    44.                 for (var i = 0; i < commands.Length; i++)
    45.                 {
    46.                     tmp = commands[i].Split('=');
    47.  
    48.                     if (tmp.Length != 2)
    49.                         continue;
    50.  
    51.                     property = tmp[0].Trim();
    52.                     value = tmp[1].Trim();
    53.  
    54.                     if (property == "Name")
    55.                         m_PlayerStats.Name = value;
    56.  
    57.                     if (property == "Faction")
    58.                         m_PlayerStats.Team = value == "Team1" ? PlayerTeam.Team1 : PlayerTeam.Team2;
    59.  
    60.                     if (property == "Capitain")
    61.                         m_PlayerStats.IsCapitain = int.Parse(value) > 0;
    62.                 }
    63.             }
    64.  
    65.             CmdUpdateBaseStats(m_PlayerStats.Name, m_PlayerStats.Team, m_PlayerStats.IsCapitain);
    66.         }
    67.  
    68.         name = string.Format("Player ({0})", isLocalPlayer ? "Local" : "Replicated");
    69.     }
    70.  
    71.     [Command]
    72.     private void CmdUpdateBaseStats(string name, PlayerTeam team, bool isCapitain)
    73.     {
    74.         UpdateBaseStats(name, team, isCapitain);
    75.         RpcUpdateBaseStats(name, team, isCapitain);
    76.     }
    77.  
    78.     [ClientRpc]
    79.     private void RpcUpdateBaseStats(string name, PlayerTeam team, bool isCapitain)
    80.     {
    81.         UpdateBaseStats(name, team, isCapitain);
    82.     }
    83.  
    84.     private void UpdateBaseStats(string name, PlayerTeam team, bool isCapitain)
    85.     {
    86.         m_PlayerStats.Name = name;
    87.         m_PlayerStats.Team = team;
    88.         m_PlayerStats.IsCapitain = isCapitain;
    89.  
    90.         m_PlayerNameText.text = name;
    91.         m_PlayerFactionText.text = team.ToString();
    92.  
    93.         if (isCapitain)
    94.             m_PlayerFactionText.text += "++";
    95.     }
    96.  
    97.     [Client]
    98.     private void SetEnabled<T>(bool isEnabled = true) where T : Behaviour
    99.     {
    100.         m_CameraNode.GetComponent<T>().enabled = isEnabled;
    101.     }
    102. }
    Do you see something strange here?

    Thanks for your help
     
unityunity