Search Unity

Player isn't ready for ClientRpcs

Discussion in 'Multiplayer' started by DBFace, Jul 13, 2017.

  1. DBFace

    DBFace

    Joined:
    May 22, 2016
    Posts:
    18
    Hello everybody !
    Well I know that I'm not the first one with that type of problems but basics solutions are not working either !
    First I have to present you my code :
    PlayerSetup script
    Code (CSharp):
    1. public class PlayerSetup : NetworkBehaviour {
    2.  
    3. void Start()
    4. {
    5.   if(isLocalPlayer
    6.   {
    7.       GetComponent<Player>().Setup();
    8.   }
    9.   else
    10.   {
    11.       //Do something else
    12.    }
    13. }
    14. }
    Player Script
    Code (CSharp):
    1. public class Player : NetworkBehaviour {
    2.  
    3. public void Setup()
    4. {
    5.    CmdSetup();
    6. }
    7. [Command]
    8. void CmdSetup()
    9. {
    10.    RpcSetup();
    11. }
    12.  
    13. [ClientRpc]
    14. void RpcSetup()
    15. {
    16.    //Isn't call
    17. }
    18. }
    I know that starting by Start method in Unet isn't something great but it works when I directly start the game with a NetworkManager and a NetworkManagerHUD.
    But now i'm using a NetworkLobbyManager and this very important Rpc isn't working.
    I tried to replace the Start method by OnStartLocalPlayer but the result is the same.
    I also tried OnStartClient but "isLocalPlayer" is still false at this point.
    I actually don't know where I'm supposed to call this method.
    (I tried with OnLobbyServerSceneLoadedForPlayer in my NetworkManager but "isLocalPlayer" is false)

    Can someone please help me ?
    Thanks a lot.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Start and OnStartClient is too early for the messages to pass.
    OnStartLocalPlayer might be what you're after. Or you can try setting up a coroutine that waits for the initialization of network to happen, and then do your Cmd's.
     
  3. DBFace

    DBFace

    Joined:
    May 22, 2016
    Posts:
    18
    Yes I was thinking of a Coroutine but What do I have to wait for ? NetworkServer.active ?
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I would try OnStartLocalPlayer, because it should be doing what you want, and should be used for initialization.
    I haven't tried doing Coroutine initialization, but NetworkServer.active is not what you want. Since it returns whether server is active on current machine basically.

    I would've tried setting up SyncVar on server, and wait for it to set true, like:

    Code (CSharp):
    1. // Some Manager class:
    2. ...
    3.  
    4. [SyncVar]
    5. private bool ServerReady;
    6.  
    7. public override void OnStartServer(){
    8.      // This should be somewhere else
    9.      ServerReady = true;
    10. }
    11.  
    12. ...
    Overall, try OnStartLocalPlayer first.
     
  5. DBFace

    DBFace

    Joined:
    May 22, 2016
    Posts:
    18
    OnStartLocalPlayer doesn't work (at least "isLocalPlayer" is set). I will try to put à SyncVar "serverReady" in my NetworkLobbyManager
     
  6. DBFace

    DBFace

    Joined:
    May 22, 2016
    Posts:
    18
    Well sorry everybody but it isn't working. OnStartServer seems to be call in the lobby. (Futhermore, I can't put a SyncVar in my NetworkLobbyManager because "MyLobbyNetworkManager is not a NetworkBehaviour').
    But anyway, now I did some tests and now I'm sure that it's a question of timing because if I put my setup in a Coroutine and I wait for 3 seconds, everything works fine !
    So yes I could leave it and put a loading menu for the 3 seconds but I think it is a little bit barbaric.