#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)

sudden stag
#

May I ask how to implement in Mirror to prevent other players from joining once the game has officially started? Let's say the host invites a player via steam, then the host has already started the game, and the invited player is late, I request that this player will not be able to connect to the host

sour yoke
#

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

sudden stag
sour yoke
sudden stag
sour yoke
#

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.

sudden stag
sour yoke
#

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.

sudden stag
sour yoke
#

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.

sudden stag
#
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.

sudden stag
sour yoke
#

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.

sudden stag
sour yoke
#

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.

sudden stag
# sour yoke When Mirror switches to the offline scene you get a new network manager which wi...
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