I have a game that when player login to the game, they will host themself and enter “scene A”. Then I use Netcode for Gameobject service “Matchmaking” to matchmake “Player A” with “Player B”.
I tried to Shutdown the Host by player and connect to deliciated server by StartClient(). But I got error “Cannot start client while an instance is already running”.
One thought: I tried to link my problem with when the game have multiple lobby and join one game server, how pro do it ? Like PUBG, each room have 4 player and move to big scene. I mean how “pro” handle shutdown lobby and join big game scene ?
Thanks for advice !
public bool StartMyClient()
{
if (NetworkManager.Singleton.IsConnectedClient)
{
NetworkManager.Singleton.Shutdown();
}
var success = NetworkManager.Singleton.StartClient();
if (success)
{
//NetworkManager.Singleton.SceneManager.OnSceneEvent += SceneManager_OnSceneEvent;
IsClient = true;
BindSceneEvent();
}
return success;
}
public bool StartMyClient()
{
if (NetworkManager.Singleton.IsConnectedClient)
{
NetworkManager.Singleton.Shutdown();
}
I’ve seen this a couple times before. It is telling that when you call this method, the application is not fully aware of its state and hasn’t properly done a shutdown of the previous session.
Normally, you’d expect a running session to have ended well before a new one starts - let’s say the user is ingame playing online. Then the user quits the game session, this automatically shuts down netcode and returns to the main menu. From the main menu the user choses to participate in another network session.
I have yet to see a game that lets you start a new online session from within a running session without returning to the menu in between.
The issue you get - just a hunch - could be related to the fact that you’re shutting down and instantly try to start it anew. It may take netcode some background processing until it is fully shutdown.
Try checking the NetworkManager state right after shutdown. IsListening still true?
Perhaps the shutdown method is awaitable? If so, await it.
If not, try adding a coroutine with yield return null once or twice in between to test my hypothesis.
Thank you a lot! Help me reconsider my network flow and I think i did it wrong.
You are right that try to start a new client right after shuting down will cause some hidden error.
Just one more quest please. When I want to move my party (lobby) to game scene. Can I just disconnect them all then move them to the “big game scene” ?
I haven’t used Lobby yet (I hope to get to that soon).
I tend to assume that once players are in a Lobby, the Host starts the game and all clients get a callback with info on how to join that game session. Afterwards I believe the Lobby is either destructed or kept alive, depending on whether players may want to return to the same Lobby.