Search Unity

RPC and Cmd don't work like should?

Discussion in 'Multiplayer' started by Oskar_Kasprzak, Jul 24, 2017.

  1. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    Hello there.

    So I got simple task here, I want to change scene when someone press a button, even client. So here is my code which both of players have attached:
    Code (csharp):
    1.  
    2.     [Command]
    3.     public void CmdRestartLevel() {
    4.         Debug.Log("cmdCalled");
    5.         RpcRestartLevel();
    6.     }
    7.  
    8.    [ClientRpc]
    9.     void RpcRestartLevel()
    10.     {
    11.         Debug.Log("RpcCalled");
    12.     networkM.ServerChangeScene("scene");
    13. }
    14.  
    It works for host, but when I press it on client it says "RPC function called on client"

    Let's point out few things:
    This piece of code is in script which is attached to GameController GameObject.
    GameController has Network Identity with Local Player Authority
    Canvas has Network Identity with Local Player Authority
    Button (from Canvas) which is calling "CmdRestartLevel() has Network Indentity with Local Player Authority.

    Anyone who could help or explain it a bit? I was looking for solution but AFAIK that's how you're supposed to call Rpc from client (via Cmd).

    Best regards.
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    What method is the client calling? Clients should use the Command method and Server should use the Rpc.
     
  3. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    @TwoTen Thanks for reply.

    Correct me if I am wrong, but I was thinking that you can put Cmd and both host and client can use it to execute this command. According to BluetoothBoy's reply from https://forum.unity3d.com/threads/invoke-method-on-players-command-vs-clientrpc.335780/ his code should work, shouldn't it?

    So basicaly when you press UI button you get "CmdRestartLevel()" called. It's same button for client and host. When I press it at host, it works. Both client and host are restarting level. When I press it as client it says that "RPC was called from client" (which shouldn't happed according to BluetoothBoy's answer)

    I changed my code a bit. Now it's look like this and clicking UI button is executing "RestartLevel()".

    Code (csharp):
    1.  
    2.  [Command]
    3.     public void CmdRestartLevel() {
    4.         Debug.Log("cmdCalled");
    5.         networkM.ServerChangeScene("scene");
    6.     }
    7.     [ClientRpc]
    8.     public void RpcRestartLevel()
    9.     {
    10.         Debug.Log("RpcCalled");
    11.         networkM.ServerChangeScene("scene");
    12.     }
    13.     public void RestartLevel() {
    14.         if (isServer) {
    15.             RpcRestartLevel();
    16.         }
    17.         else {
    18.             CmdRestartLevel();
    19.         }
    20.     }
    21.  
    And now I get warning (not an error) that "Trying to send command for object without authority" when client tries to press button, but again, it's working when host does so.

    Warning is pointing at line where CmdRestartLevel(); is called from RestartLevel() function:
    Code (csharp):
    1.  
    2.   public void RestartLevel() {
    3.         if (isServer) {
    4.             RpcRestartLevel();
    5.         }
    6.         else {
    7.             CmdRestartLevel();
    8.         }
    9.     }
    10. }
    But what object doesn't have authority? GameController, Canvas, Buttons have Player Authority set in editor.

    Best regards.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I don't think you should be manually changing scenes when using NetworkManager. It supposed to be handled by NetworkServer. All you need to do is to change it on server, I think. (never tried this though)
     
    TwoTen likes this.
  5. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    Yeah, I found it already, but thanks for answer.