#May I ask how to implement in Mirror to prevent other players from joining once the game has officia
24 messages · Page 1 of 1 (latest)
When starting the game set NetworkServer.maxConnections = NetworkServer.connections.Count(). Mirror will reject any new connections
You can also do that in custom network manager OnServerDisconnect after calling base.OnServerDisconnect(conn); to reset it lower so players can't rejoin.
@sudden stag
Is this standard practice?
If a player drops out, won't the one who arrived late succeed in joining the host by mistake?
See my 2nd msg about OnServerDisconnect...that would apply to any new connection attempts, rejoiners or late joiners.
But the second one also means that dropped players will no longer have the opportunity to reconnect back to the host either... doesn't Mirror support dropped players getting back into the game?
You have to roll your own for that...a new connection is just a new client until authenticated and recognized in your code as having been connected at some point previously.
Network Authenticator would be good for that, but then you can't use the maxConnections setting to block connections. You'd have to let the client connect, authenticate, then kick them out if you don't want them in.
Is there a Mirror example I can refer to?
Start here and look at the NA's that are included with Mirror. Also the Chat example has a name authenticator that shows how to do a delayed disconnect if the player name is already in use (can be any criteria you want).
Your custom NA from our Templates could just add steam ID from the conn to a static list and accept all until a gameStarted bool is set true, then for new conneections their steam ID would be checked against that static list, and if it's already there, accept, otherwise send them a refusal message and kick them.
Thank you very much for your help.
Maybe the hard part is how to recover this client's data, especially when the game is in host mode.
server side static Dictionary with steam id as key and struct of data you need to re-apply to their player in OnStartServer overrides of various components.
Set that data in OnServerDisconnect before calling base.OnServerDisconnect(conn) in case they come back. conn.identity.GetComponent to pull the data out of the player
Obviously if the host bails everything is gone and the other clients have to start over. Don't even ask me about host migration...if steam can't help you, get a dedicated server.
public override void OnServerDisconnect(NetworkConnectionToClient conn)
{
if (Utils.IsSceneActive(SceneName.Game.ToString()))
{
NetworkServer.maxConnections--;
if (NetworkServer.maxConnections == 1 || NetworkServer.connections.Count == 1)
{
maxConnections = 3;
}
}
base.OnServerDisconnect(conn);
}
@sour yoke The host may be the last to exit the game and return to the offline scenario, maybe the logic has to be arranged that way.
My game is for up to 3 players.
Don't worry, I'm not thinking about host migration for now
NetworkServer.maxConnections, not Network Manager
When Mirror switches to the offline scene you get a new network manager which will reset network server to the original maxConnections setting.
What is the difference between NetworkManager.singleton.maxConnections and NetworkServer.maxConnections?
Should I also use NetworkServer.maxConnections = NetworkServer.connections.Count at the start game?
NetworkManager.singleton.maxConnections is whatever you set at design time. That's only passed along to NetworkServer.maxConnections once when the server starts...changing it in Network Manager after that does nothing to NetworkServer
Set NetworkServer.maxConnections = NetworkServer.connections.Count when host starts the game, as I said earlier. If you reduce it further in OnServerDisconnect, no one will be able to come back.
public void ExitServer()
{
if (NetworkServer.activeHost)
{
NetworkManager.singleton.StopHost();
}
else
{
NetworkManager.singleton.StopClient();
}
SceneManager.LoadSceneAsync(SceneName.Logo.ToString());
}
I took a look at my code and I am manually going back to the offline scene (i.e. Logo). Looking at NetworkManager's DontDestroy, it doesn't seem like it will be the new one, it's still the old one