Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Limiting the maximum number of players (NetworkDriver)

Discussion in 'NetCode for ECS' started by OrientedPain, Jul 4, 2023.

  1. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    37
    Hello Everyone,

    I want to make a connection approval process that can reject incoming connections based on any number of connected players. How can I limit the number of players (Clients) connecting the game (Server)? Does network driver count the connected players or should I do my own counter on server side? How can I use ServerClientCount in Bootstrap (ClientServerBootstrap.WorldCounts.Data.serverWorlds
    Code (CSharp):
    1. ClientServerBootstrap.WorldCounts.Data.serverWorlds
    )

    Code (CSharp):
    1. public static void RegisterServerDriver(World world, ref NetworkDriverStore driverStore, NetDebug netDebug, ref FixedString4096Bytes certificate, ref FixedString4096Bytes privateKey, int playerCount = 0
    2.         {
    3.             var settings = GetNetworkServerSettings(playerCount: playerCount);
    4.             settings = settings.WithSecureServerParameters(certificate: ref certificate, privateKey: ref privateKey);
    5.             RegisterServerDriver(world, ref driverStore, netDebug, settings);
    6.         }
    Thanks in advance.
     
  2. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    224
    Hey OrientedPain!

    Unfortunately there is currently no way to limit player count via UTP directly. You'll need to:
    1. Accept the connection.
    2. Immediately close it again (via the NetworkConnection entity) based on connected player count. Optionally after sending an RPC containing a reason for the server kick.
    Relatedly, you may find the following thread valuable, depending on the kind of security you need for your game:
    https://forum.unity.com/threads/con...hots-for-spectator-mode.1448041/#post-9092248
     
    PolarTron and OrientedPain like this.
  3. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    37
    Thank you so much NiKi, you're awesome as usual :)
     
    NikiWalker likes this.