Search Unity

Bug with ClientRpc

Discussion in 'Multiplayer' started by RoyDeFronce, Sep 18, 2017.

  1. RoyDeFronce

    RoyDeFronce

    Joined:
    Nov 4, 2016
    Posts:
    3
    Hello,

    I encounter a sync problem on a certain ClientRpc function which does not want to be done on the client side.

    To resume the code, in the multiplayer scene I have an object that contains the script "MultiUIManager" wich its start function will only be used by the server and that will recover all the players in the scene.
    In the recovery function at certain times RpcClient functions will be called, but the problem is that these RpcClients functions are not called client side and I do not understand why ( when i try a lot of time, sometimes it's works ).

    I made a little schema in case:



    For the code in MultiUIManager :

    Code (csharp):
    1.  
    2. void Start ( )    
    3. {        
    4.      if ( isServer )        
    5.      {          
    6.            paw.getPlayer ( );        
    7.      }    
    8. }
    9.  
    10.  

    In PawControlMulti


    Code (csharp):
    1.  
    2. [Server]
    3.     public void getPlayer ( )
    4.     {
    5.         nbrPlay.Clear ( );
    6.  
    7.         List<PlayerMulti> getP = allPlayer;
    8.         List<int> getNbrP = nbrPlay;
    9.  
    10.         int nbrPlayer = 0;
    11.  
    12.         foreach ( PlayerMulti thisPlayer in FindObjectsOfType<PlayerMulti> ( ) )
    13.         {
    14.             getP.Add ( thisPlayer );
    15.             getP [ nbrPlayer ].IdPlayer = nbrPlayer;
    16.             getP [ nbrPlayer ].CanDetect = false;
    17.             getP [ nbrPlayer ].CanPlay = false;
    18.  
    19.             getNbrP.Add ( 0 );
    20.  
    21.             RpcIniPlayer ( thisPlayer.gameObject, nbrPlayer );
    22.  
    23.             nbrPlayer ++;
    24.         }
    25.  
    26.         if ( nbrPlayer < Prototype.NetworkLobby.LobbyManager.s_Singleton.numPlayers )
    27.         {
    28.             getP.Clear ( );
    29.             getNbrP.Clear ( );
    30.             canOpen = false;
    31.  
    32.             RpcClearNbrP ( );
    33.  
    34.             StartCoroutine ( waitRecheck ( ) );
    35.             return;
    36.         }
    37.  
    38.         Debug.Log ( "server in PawMulti" );
    39.  
    40.         RpcPassPlayer ( nbrPlayer );
    41.  
    42.         currentPlayer = Random.Range ( 0, nbrPlayer );
    43.  
    44.         enableAPlayer ( );
    45.         canOpen = true;
    46.  
    47.         MultiUIManager.MUIManager.StartIniPlayer ( getP );
    48.     }
    49.  
    50.  
    And for the 3 RpcClients fonction :

    Code (csharp):
    1.  
    2. [ClientRpc]
    3.     void RpcIniPlayer ( GameObject thisPlayer, int thisID )
    4.     {
    5.         Debug.Log ( "Client in RpcIniPlayer" );
    6.         PlayerMulti thisP = thisPlayer.GetComponent <PlayerMulti> ( );
    7.         allPlayer.Add ( thisP );
    8.         nbrPlay.Add ( 0 );
    9.  
    10.         thisP.IdPlayer = thisID;
    11.         thisP.CanDetect = false;
    12.         thisP.CanPlay = false;
    13.     }
    14.  
    15.     [ClientRpc]
    16.     void RpcClearNbrP ( )
    17.     {
    18.         Debug.Log ( "Client in ClearNbrP" );
    19.         allPlayer.Clear ( );
    20.         nbrPlay.Clear ( );
    21.     }
    22.  
    23.     [ClientRpc]
    24.     void RpcPassPlayer ( int nbrPlayer )
    25.     {
    26.         Debug.Log ( "Client in RpcPassPlayer" );
    27.         List<Image> setPass = passPlayer;
    28.  
    29.         Vector2 rectParent = getPassInfo.GetComponent<RectTransform> ( ).sizeDelta;
    30.         Vector2 thisRect;
    31.  
    32.         GameObject getCurrImg;
    33.  
    34.         float getSpace = ( float ) ( rectParent.x - PassPrefb.GetComponent<RectTransform> ( ).sizeDelta.x * nbrPlayer ) / ( nbrPlayer - 1 );
    35.         int a;
    36.  
    37.         for ( a = 0; a < nbrPlayer; a++ )
    38.         {
    39.             getCurrImg = ( GameObject ) Instantiate ( PassPrefb, getPassInfo );
    40.             thisRect = getCurrImg.GetComponent<RectTransform> ( ).sizeDelta;
    41.             getCurrImg.GetComponent<Transform> ( ).localPosition = new Vector2 ( -rectParent.x / 2 + thisRect.x / 2 + a * thisRect.x + getSpace * a, rectParent.y / 2 - thisRect.y / 2 );
    42.  
    43.             getCurrImg.GetComponent<Image> ( ).sprite = StopPass;
    44.             setPass.Add ( getCurrImg.GetComponent<Image> ( ) );
    45.         }
    46.     }
    47.  
    As a result none of its three RpcClients functions want to work, while those used in the player script work.
    I do not know if it is due to the fact that the client does not initialize in time? but it would seem odd to me that I can recover all the players in my variable List (and there is a check if there is lack of players).
    And I hope this is not due to the fact that the object is present by default in the multi scene, it would be a bit abused.
    Finally, if a few to a track it would help me greatly;
    Thank you.
     
  2. Deleted User

    Deleted User

    Guest

    Try to change Start() to public override OnStartServer();
     
  3. RoyDeFronce

    RoyDeFronce

    Joined:
    Nov 4, 2016
    Posts:
    3
    it's not working either ;s

    i don't know if base.OnStartServer is usefull but i tried with and without this.
     
  4. RoyDeFronce

    RoyDeFronce

    Joined:
    Nov 4, 2016
    Posts:
    3
    I finaly resolves this.

    The problem was the client observer was not initialise too late, and when i host directly on unity, is not working (probably the firewall).

    For the observer i just add an IEnumerator and i compare the number of observers and the number of player with the lobbyManager.

    Thanks for the help