How to properly set a players limit / maximum for a UNET server

Hey everyone,

I want to know how to properly set a limit for maximum players with UNET games while using the Network Manager and without using Matchmaking. For example, let’s assume the limit is supposed to be for LAN games.

I thought that the proper way to do this would be to just set the maxConnections attribute on the server. However, if I use a value of 1, I noticed that this does not prevent a second client from connecting to the game.

How do you guys solve this issue in your projects?

Thanks in advance!

I also had the same problem where setting maxConnections seemed to have no effect. Unsure why.

However using the overloaded StartHost(…, maxConnections) seems to work.

Can you elaborate a bit more on how it works out in your scenario? What happens exacly?

I’ve just did some tests with the overloaded StartHost and even when setting maxConnections to 1, the 2nd connection is still accepted and processed. Also, there’s no automatic disconnect or something.

I am using LAN host/client (not relay server). If more clients try and connect then are allowed it prevents them… but unsure what is going on behind the scenes.

Here’s my code.

  public void BatStartLocalHost()
  {
  StartHost(MakeNewConnectionConfig(), GameController.maxPlayers-1); //maxConnections is the number of EXTERNAL connections (in a 3 player game there are only 2 external connections)
  }

  public void BatStartLocalClient()
  {
  StartClient(null, MakeNewConnectionConfig());
  }

  private ConnectionConfig MakeNewConnectionConfig()
  {
  ConnectionConfig connectionConfigTemp = new ConnectionConfig();
  connectionConfigTemp.AddChannel(QosType.ReliableSequenced);
  connectionConfigTemp.AddChannel(QosType.Unreliable);
  connectionConfigTemp.AddChannel(QosType.Reliable); //reliable unsequenced
  return connectionConfigTemp;
  }
2 Likes

Thanks Zullar, I’ve managed to get it to work. :slight_smile:

In fact, I was not aware that maxConnections includes external connections only. Also, I’ve learned that using a value of zero is not a proper solution to enforce singleplayer mode - the maxConnections value always needs to be at least 1.

BTW, it also seems to work without using the connectionConfig parameter, but with just calling:

StartHost (null, maxConnections);

@ Devs, the overloaded StartHost functions should be added to the official UNET docs.

2 Likes