Search Unity

Lobby Manager and ClientRpc

Discussion in 'Multiplayer' started by Xuzon, Jul 20, 2015.

  1. Xuzon

    Xuzon

    Joined:
    Mar 21, 2014
    Posts:
    83
    Hello, I have a problem here, I have a script like this in the player:
    Code (CSharp):
    1.  
    2. void Start(){
    3.     if (isLocalPlayer){
    4.         Cmd_call_print();
    5.     }
    6. }
    7.  
    8. [Command]
    9. void Cmd_call_print(){
    10.     Rpc_print();
    11. }
    12.  
    13. [ClientRpc]
    14. void Rpc_print(){
    15.     print("Hello");
    16. }
    When a player spawn and do his Start() he prints "hello" in all clients (including host). I have no problem with this script when I use the NetworkManager, but if I use it with the NetworkLobbyManager the host can't send the ClientRpc, and if I try to call it whenever I want, the host can't send the Rpc but the clients calling the Cmd_call_print can call the Rpc_print().

    I have no clue what happens here so I need help please.
     
  2. Anisoropos

    Anisoropos

    Joined:
    Jul 30, 2012
    Posts:
    102
    Same issue here - even while using the provided Lobby Example from the asset store, clients calling Cmd will trigger an Rpc but if a client (who also happens to be the host) calls a Cmd, nothing happens.

    Found a workaround - but it scales horribly (having a lot of Cmd / Rpc pairs will result in unreadable code). Here it is:

    Code (CSharp):
    1. void Start()
    2. {
    3.     if (isLocalPlayer)
    4.         CmdPrint();
    5. }
    6. [Command]
    7. void CmdPrint()
    8. {
    9.     // For the other clients
    10.     RpcPrint();
    11.  
    12.     // For the server itself
    13.     Print();          
    14. }
    15.  
    16. [ClientRpc]
    17. void RpcPrint()
    18. {
    19.     Print();
    20. }
    21.  
    22. void Print()
    23. {
    24.     Debug.Log("Hi");
    25. }
     
  3. Anisoropos

    Anisoropos

    Joined:
    Jul 30, 2012
    Posts:
    102
    [UPDATE]

    Changing the attribute [ClientRpc] to the deprecated [RPC], inverses the problem: Servers get the Rpc, but Clients do not.
    Unfortunately the results are the same if you stack the attributes.
     
  4. Anisoropos

    Anisoropos

    Joined:
    Jul 30, 2012
    Posts:
    102
    [DIRTY_FIX]

    This seems after all to be related to the Player's Network Identity's Observers - the host object doesn't have itself in that list - therefore it is not receiving Rpc's. The fix is quite simple:


    Code (CSharp):
    1. foreach (NetworkIdentity nI in FindObjectsOfType<NetworkIdentity>())
    2.     nI.RebuildObservers(true);
    The problem is placing this code - ideally it should be placed whenever a new player is connected. Good luck :)
     
  5. Xuzon

    Xuzon

    Joined:
    Mar 21, 2014
    Posts:
    83
    Thanks a lot man!! but unfortunately I have made my own lobby with the network manager, is a bit complex but it worked so.. XD
     
    Anisoropos likes this.
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    There was a bug with observers on scene objects. Try upgrading to latest patch release.
     
    Anisoropos likes this.