Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Limiting Max Connections to 2 (Coop)?

Discussion in 'Multiplayer' started by LootlabGames, Apr 5, 2018.

  1. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    340
    I am trying to limit the max connections to 2 for coop.

    I have been struggling with this for more than a week now.
    I have read 100's of useless posts that don't work.
    I can't believe something that should be so simple is not.

    I am using UNET LAN not matchmaking.

    I have tried setting the max connections to 2 under the NetworkManager.
    I have tried using StartHost(null, 2);
    I have tried this: https://forum.unity.com/threads/limiting-players-on-server.429785/
    It was the closest but the problem is unity still loads the scene because the ONServerConnect is after it has already started loading the scene(Or so it seems).

    I'm really at a loss here any help is appreciated.
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,117
    The network manager can be problematic or lack control in some cases. I would suggest you implement the manager yourself by using NetworkServer and NetworkClient classes. Then you have to handle messages like MsgType.connect and MsgType.Disconnect yourself but you have full control.

    There might be a way to achieve this with the network maanger or at least with modifications to its source code but I'm not familiar with it that much. As i understand your issue is that if a third player tries to connect to the game then the game scene will be loaded before connecting fails right?
     
  3. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    340
    Thanks for your reply.

    Yes, what I would like to happen is this:

    Player #3 tries to connect.
    When connected a check is done to see how many players are currently connected.
    If currentPlayerCount >= maxPlayerCount then disconnect the player, don't load the scene and return back.

    Using what I found here: https://forum.unity.com/threads/limiting-players-on-server.429785/

    Code (CSharp):
    1.     //Register the connection rejection
    2.     public override void OnStartClient(NetworkClient client) {
    3.         base.OnStartClient(client);
    4.         client.RegisterHandler(Messages.ServerFull, ClServerFull);
    5.     }
    6.     /*
    7.      * SERVER
    8.      */
    9.     /// <summary>
    10.     /// Send this to reject a connection from the server in case of server full.
    11.     /// </summary>
    12.     /// <param name="conn"></param>
    13.     /// <returns></returns>
    14.     IEnumerator RejectConnection(NetworkConnection conn) {
    15.         yield return new WaitForEndOfFrame();//Must wait one frame (Unity known bug)
    16.         conn.Send(Messages.ServerFull, new EmptyMessage());
    17.     }
    18.     // called when a client connects
    19.     public override void OnServerConnect(NetworkConnection conn) {
    20.         //Reject player if max
    21.         if(GetPlayerControllers().Count >= maxPlayers) {
    22.             StartCoroutine(RejectConnection(conn));
    23.             return;
    24.         }
    25.     }
    I almost get it working but the problem is even if I try to disconnect the player in the method ClServerFull the scene still loads.
     
  4. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,117
    First of all setting max connections to two should automatically prevents you from connecting and you should be disconnected with error NetworkError.TooManyConnections .

    And secondly about the code, it means the NetworkManager will load the scene as soon as it connects so that happens, why it connects even if you set max server connections to 2? honestly it shouldn't and either it is a bug or you are setting it up incorrectly.

    I don't use the NetworkManager and always use NetworkServer and NetworkClient unless i want to create a minimal project for showcasing a bug or ...
     
  5. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    340
    You are correct that is what I would expect it to do.
    But it is not what it does at all.
    If I set it to 2 I have been able to connect 3 (possibly more, I never tried) players.
     
  6. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,117
    Well then submit a bug report and maybe for now avoid using the network maanger, i have a tutorial on Assetstore which describes how to do it and it is 40% off until 12th, If the price is high however, send me message and i'll give you a code to get it for free.
     
  7. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    340
    It's more than I can spend at the moment. But it you are offering a code It would be appreciated.
     
  8. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,117
    Yup sure, Sent it as a private messge to you. Go ahead and get it and i hope you learn a thing or two from it.