Search Unity

Network lobby wait until all player scenes have loaded

Discussion in 'Multiplayer' started by Griffo, Jul 24, 2018.

  1. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Hi,
    I'm a noob to networking and a bit stuck with network lobby, my problem is if i build and load a scene on say 3 devices if the server one is the fastest it will load the scene and instantiate the player into the scene while the other 2 can be another 4 seconds behind loading, then they load their scenes and the player are instantiated into the scene.

    What i'm trying to do is get the players state at startup and add it to a list of bool's 'playersReady' false and as each one is ready set their bool to true and only when all are true get the server to instantiate the players into the scene.

    I've jumped into LobbyManager.cs and added the below code to get the count of connected players and their 'playersReady' state that is set to false.

    Code (CSharp):
    1. public int playerCount;
    2. public List<bool> playersReady = new List<bool>();
    3.  
    4. public override void OnServerConnect(NetworkConnection conn)
    5. {
    6.     playerCount = 0;
    7.     StartCoroutine(playerReadyScript.PlayerCount(0));
    8.     foreach (NetworkConnection con in NetworkServer.connections)
    9.     {
    10.         if (con != null)
    11.         {
    12.                 playerCount++;
    13.         }
    14.     }
    15.         base.OnServerConnect(conn);
    16. }
    17.  
    18. public override void OnClientNotReady(NetworkConnection conn)
    19. {
    20.     playersReady[playerCount-1] = false;
    21.     base.OnClientNotReady(conn);
    22. }
    23.  
    Then when the player is ready i use this to set playersReady to true.

    Code (CSharp):
    1. public override void OnClientSceneChanged(NetworkConnection conn)
    2. {
    3.     playersReady[playerCount-1] = true;
    4. }
    I'm very confused how to go about this, i've googled for days without finding an acceptable way of doing it, is there away of accessing the server code to add a while loop checking the playersReady List until all have been set true then letting the server send the message to load their scene? if i use this test code by setting a bool true with a key press

    Code (CSharp):
    1. public override void OnClientSceneChanged(NetworkConnection conn)
    2. {
    3.     StartCoroutine(Waiting(conn));
    4. }
    5.  
    6. public IEnumerator Waiting (NetworkConnection conn)
    7. {
    8.     while (waiting)
    9.     {
    10.          yield return null;
    11.     }
    12.     base.OnClientSceneChanged(conn);
    13. }
    that will hold at the lobby screen until 'w' is pressed setting 'waiting' false, but the scene has still loaded and the player instantiated into the scene, i just want to stop the message being sent to load their scene until 'w' is pressed, again any help or pointing me in the right direction would be gratefully appreciated.