- Home /
Connection already ready when adding players to Host
Working on a game which will have three different game modes, two which are single device and a third which allows access through the network. In order to simplify the game code its been structured to have a single scene with networking identities on all relevant objects. In doing so, I'm setting up a custom NetworkLobbyManager which can be used regardless of game mode.
The error I'm seeing is when attempting to add local players, specifically Lobby Players, to a started host. The process flow is below:
public class MyLobbyManager : NetworkLobbyManager {
public void StartSingleHost() {
maxConnections = 1;
maxPlayers = 5;
maxPlayersPerConnection = 5;
autoCreatePlayer = false;
StartHost();
}
}
This configures and starts the host. On the subsequent frame, I'm attempting to add two local device players via:
MyLobbyManager lobbyMgr = FindObjectOfType<MyLobbyManager>();
lobbyMgr.TryToAddPlayer();
This performs as expected; however Unity throws the error: "A connection has already been set as ready. There can only be one."
I've tried with several different approaches:
ClientScene.AddPlayer ~ Same error
NetworkServer.AddPlayerForConnection ~Different error, I don't recall the exact messaging
My original attempt is what is currently listed. After no results I used .NET Reflector to inspect how the NetworkLobbyManger had this setup. The key areas of interest in the NetworkLobbyManager are:
public class NetworkLobbyManager : NetworkManager {
public override void OnStartServer() {
if (this.lobbySlots.Length == 0)
this.lobbySlots = new NetworkLobbyPlayer[this.maxPlayers];
this.OnLobbyStartServer();
}
public virtual void OnLobbyStartServer() {}
public override void OnStartHost(){
this.OnLobbyStartHost();
}
public virtual void OnLobbyStartHost() { }
private void OnGUI() {
...
this.TryToAddPlayer();
}
}
The reference I found regarding this issue showed that a simple check is being made to see if the connection already exists and then exists, with an error logged, if that is the case. As queried in the reference and based upon my own inspection, it doesn't look like this impacts anything; however, any errors in the log bother me. Inspecting further it does look as if StartHost() calls ConnectLocalClient() but tracing that is a veritable rats nest.
Is there a preferred method of starting a local host or connecting local players? Considering an implementation using a NetworkLobbyManager and the NetworkMangerHUD don't yield the same results when adding players I can't imagine this is a defect within Unity, unless there's something within the NetworkLobbyManager which is not available in the subclass. Now that I think of it, I did notice a similar, unresolved, issue when attempting to extend the NetworkLobbyPlayer.
Any insight into the matter would be greatly appreciated.
Answer by BMS-CB · Oct 02, 2015 at 03:00 PM
It looks like this may be a result of a delay between when the Host is started and the Client becomes ready. I had tried individual and successive calls to:
if (ClientScene.ready) {
ClientScene.AddPlayer(controllerId);
}
else {
ClientScene.AddPlayer(NetworkClient.allClients[0].connection, controllerId);
}
Always with the same error message. After moving on and trying to resolve another issue with stopping and restarting the Host, I noticed that the error was no longer occurring. Moving the player creation logic to the Update method resolved the error. More specifically, I threw it behind:
public void Update() {
if (!doneAddingPlayers && ClientScene.ready) {
AddPlayer(controllerId);
AddPlayer(controllerId);
doneAddingPlayers = true;
}
}
I'll be doing some more inspection and if timing is the issue, creating a callback for player creation and adding that to MyLobbyManager once the client is ready.
Hey, just wondering if the callback creation fixed your issue.
I'm also getting this error with the caveat that it ONLY happens if Network$$anonymous$$anager.Shutdown() was called at some point prior to visiting the scene with the Network$$anonymous$$anager.
For example, I enter the lobbyScene and call StartHost(), no issues. BUT, if I exit the lobby, kill the NetworkLobby$$anonymous$$anager instance, and call Network$$anonymous$$anager.Shutdown(), THEN I get the error (though it doesn't impact functionality in any way that I could see).
I've tried commenting out various overrides in the network lobby manager, but it didn't seem to make a different.
Thanks in advance.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Client can't spawn GameObject's 0 Answers
Multiplayer Moving Bullet 1 Answer
Unet error code : unity host id out of bound id -1 max id should be greater 0 and less than 2 1 Answer