Unet - Limiting the max amount of connections below 1.

Hello everyone!
First, thanks for your help on the matter.

I’m currently working with Unet (and learning a lot), I have came to face a problem.

Let’s put it this way. The game I’m building can have up to 4 players in a scene.
The game allows full controls over what should controls each of the 4 players:

• P1 can be controlled locally by a keyboard or a controller.
• P2 can be controlled locally by a keyboard or a controller or by an AI (controlled on the host, so locally if on the host PC) or a remote player’s client via LAN.
• P3 can be controlled locally by a keyboard or a controller or by an AI (controlled on the host, so locally if on the host PC) or a remote player’s client via LAN.
• P4 can be controlled locally by a keyboard or a controller or by an AI (controlled on the host, so locally if on the host PC) or a remote player’s client via LAN.

Everything seems to works fine. Controls are responding both locally and over the network whenever applicable, animation runs smoothly, but I’m still stuck with one little pickle.

It’s kinda hard to explain and whenever I looked online, there’s too much similar terms polluting the results for >20 pages. It’s a real nightmare to read every small bit just in case it cover the things I’m looking for. (I have done it for the last 2 days with barely no break except for the basic living needs). So, please, bear with me if I’m not clear enough.

As I’m building the game with, in mind, its online features as the base of the system with an Host based connection (meaning the host is both a client and a server), the obvious way of managing the single player or local multiplayer should, in theory, be to run the application as a client + server like when online, but limiting the number of client able to connect to the server to only be the host’s. This is how many coop/PvE based games do it.

Now, this is where the NetworkManager seems to fails for some reason. Whenever you set the maximum connection (through the inspector) to the minimum (in “Advanced Configuration”), the number can’t be below 1. Normally, you might guess that this is because the host has to be counted in… but it seems that it’s not the case and that is a minimum of 1 remote client (meaning not counting the host).

This brings me to wonder how can we limit the number of client that can remotely connect to the host to none?

So, let’s say the player start “host” a game. As of the moment, he got no remote friend to play with. So he wish to start playing alone against AI (or a relative play with him on the same client/application as I made the game controls system in a way that such thing is possible and all.)
If it was to about limiting the maximum amount of remote clients that can connect, that would be quite simple as I would simply limit the maximum amount of connected client to the current amount, but for some reason NetworkManager doesn’t accept something like no connection except for the host as it always leaves 1 slot opened.

I don’t think I got to explain how much it can break a game when a 5th player join a game/match that already has not open slots, right? (So, a 5th player join a hosted game which has already the host and 3 AI. The host might not want anyone to join his game, but for some reason someone is still able to join.)

Does anyone knows how to fix this kind of problem? Is there a way to automatically reject any incoming connections attempts from a host’s server when it only has the host connected to it?
I have read the whole NetworkManager documentation and the only kind of thing I found is related to closing the server (which obviously doesn’t work as I need the server to run, but only allow the host’s client to connect to it.)

Currently, the only thing that comes to my mind is to disconnect any new players that connect automatically. (So the 5th player connect and the server automatically call a disconnect function with a warning notice that the game is full.) I wonders if it’s possible to simply STOP any connection from coming in even if the host’s client is “alone” on its own server.

Thanks in advance for any feedback on the matter!

I found a solution.
I’ll share it in case others wonder how to do it.

First, you can’t set NetworkManager.singleton.maxConnections below 1 and there’s no way around it.
So if you want to set the server so that only the host client connect to it, set maxConnections to 1 through an exception method of your choice.
Personally, I set it with this :

        switch(RoomSize){
        default:
            NetworkManagerCustom.isServerOffline = false;
            if(RoomSize != NetworkManager.singleton.maxConnections){
                NetworkManager.singleton.maxConnections = RoomSize;
            }
            break;
        case 0:
            NetworkManagerCustom.isServerOffline = true;
            NetworkManager.singleton.maxConnections = 1;
            break;

        }

This won’t stop users from connecting for that single free slot, but it will adjust itself for whenever the number of online slot that is more than 0 to the correct number. The boolean isServerOffline is a static boolean I set on the script that act as my custom Network Manager called NetworkManagerCustom.cs. This is because I know that whenever something related to the network manager is being checked or called, the NetworkManagerCustom.cs will be accessible at all time and so will be that boolean value.

Now, where the hosting client press the “Start Game” or “Host Game” or whatever you called the button, it trigger this function:

    public void StartUpHost(){
        NetworkServer.dontListen = NetworkManagerCustom.isServerOffline;
        SetPort();
        NetworkManager.singleton.StartHost();
    }

Note : SetPort(); is a function I set separately that determines and set the IP and Port addresses.

NetworkServer.dontListen (which is set to false by default) is a boolean value used by the network server client (it can be either the host or separated client) which controls if the server listen or not to any incoming connections.

This will work when starting the server as it needs to be called prior to launching it and if set to true, nobody will be able to remotely connect to this host. This is how you can set a game made with multiplayer in mind into a offline/Single Player mode.

I’m currently checking how I can make the host can change it once the server is launched, but as I haven’t covered the clients and server disconnection features and behavior of my project yet, I’ll update this post once I’ll have it done. :wink: