0

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.

public int playerCount;
public List<bool> playersReady = new List<bool>();

public override void OnServerConnect(NetworkConnection conn)
{
    playerCount = 0;
    StartCoroutine(playerReadyScript.PlayerCount(0));
    foreach (NetworkConnection con in NetworkServer.connections)
    {
        if (con != null)
        {
            playerCount++;
        }
    }
    base.OnServerConnect(conn);
}

public override void OnClientNotReady(NetworkConnection conn)
{
    playersReady[playerCount-1] = false;
    base.OnClientNotReady(conn);
}

Then when the player is ready i use this to set playersReady to true.

public override void OnClientSceneChanged(NetworkConnection conn)
{
    playersReady[playerCount-1] = true;
}

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

public override void OnClientSceneChanged(NetworkConnection conn)
{
    StartCoroutine(Waiting(conn));
}

public IEnumerator Waiting (NetworkConnection conn)
{
    while (waiting)
    {
         yield return null;
    }
    base.OnClientSceneChanged(conn);
}

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.

Kevin Griffiths
  • 161
  • 2
  • 12
  • LobbyManager.cs have a function call it OnLobbyServerPlayersReady, maybe this helps – Lotan Jul 25 '18 at 07:36
  • @Lotan Hi, thanks for the input, but using OnLobbyServerPlayersReady() only holds the players in the lobby until 'w' is pressed then they are all set true and their scenes load, so the scenes still load at different time on different devices. – Kevin Griffiths Jul 25 '18 at 15:04
  • I think that what you want is impossible to check "before" the loading scene. What you have to do is maintain in "pause" the loaded scene before every player is instanciated in every client. You can check that with NetworkLobby functions or simply checking if every player have the same number of player instances as the lobby. – Lotan Jul 25 '18 at 15:15
  • 1
    I may have confused you with my description of what i'm trying to do but basically when all the players are ready in the lobby i want the server to tell them to load their scenes but i don't want the server to instantiated the players into the scenes until all the scenes have loaded. – Kevin Griffiths Jul 26 '18 at 08:19

0 Answers0