Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question New players can't join

Discussion in 'Multiplayer' started by ironcutter24, Oct 4, 2023.

  1. ironcutter24

    ironcutter24

    Joined:
    Jan 29, 2020
    Posts:
    11
    I am working on a multiplayer game that uses Netcode for GameObjecs, Game Server Hosting and Matchmaker.
    My implementation of Matchmaker is not working properly, but I can't find much more in the documentation and the sample project does pretty much what I'm doing here. I've been trying small changes for about two weeks now. Any help would be really appreciated.

    At the moment after the matchmaker allocates a server and the game starts no other players are able to join, and players that leave are not able to reconnect as well.

    This is my matchmaker config:
    Code (JavaScript):
    1. {
    2.   "Name": "AdventureWorld",
    3.   "MatchDefinition": {
    4.     "Teams": [
    5.       {
    6.         "Name": "UserTeam",
    7.         "TeamCount": {
    8.           "Min": 1,
    9.           "Max": 1
    10.         },
    11.         "PlayerCount": {
    12.           "Min": 2,
    13.           "Max": 5,
    14.           "Relaxations": []
    15.         },
    16.         "TeamRules": []
    17.       }
    18.     ],
    19.     "MatchRules": []
    20.   },
    21.   "BackfillEnabled": true
    22. }

    Game server backfill code:
    Code (CSharp):
    1.  
    2. public class Backfiller : IDisposable
    3. {
    4.     const int maxPlayers = 5;
    5.     const int ticketCheckMs = 1200;
    6.  
    7.     CreateBackfillTicketOptions backfillTicketOptions;
    8.     BackfillTicket localBackfillTicket;
    9.     MatchmakingResults matchmakingPayload;
    10.  
    11.     string externalConnectionString;
    12.  
    13.     public bool Backfilling { get; private set; } = false;
    14.  
    15.  
    16.     public Backfiller(string externalServerIp, ushort serverPort, MatchmakingResults payload)
    17.     {
    18.         externalConnectionString = $"{externalServerIp}:{serverPort}";
    19.         matchmakingPayload = payload;
    20.         NetworkManager.Singleton.OnClientDisconnectCallback += NetworkManager_OnClientDisconnected;
    21.     }
    22.  
    23.     public async Task InitBackfill()
    24.     {
    25.         var backfillProperties = new BackfillTicketProperties(matchmakingPayload.MatchProperties);
    26.         localBackfillTicket = new BackfillTicket
    27.         {
    28.             Id = matchmakingPayload.MatchProperties.BackfillTicketId,
    29.             Properties = backfillProperties
    30.         };
    31.  
    32.         await BeginBackfilling();
    33.     }
    34.  
    35.     private async Task BeginBackfilling()
    36.     {
    37.         Debug.Log("Begin backfilling...");
    38.  
    39.         var matchProperties = matchmakingPayload.MatchProperties;
    40.  
    41.         if (string.IsNullOrEmpty(localBackfillTicket.Id))
    42.         {
    43.             backfillTicketOptions = new CreateBackfillTicketOptions
    44.             {
    45.                 Connection = externalConnectionString,
    46.                 QueueName = matchmakingPayload.QueueName,
    47.                 Properties = new BackfillTicketProperties(matchProperties)
    48.             };
    49.  
    50.             localBackfillTicket.Id = await MatchmakerService.Instance.CreateBackfillTicketAsync(backfillTicketOptions);
    51.         }
    52.  
    53.         Backfilling = true;
    54. #pragma warning disable 4014
    55.         BackfillLoop();
    56. #pragma warning restore 4014
    57.     }
    58.  
    59.     private async Task BackfillLoop()
    60.     {
    61.         Debug.Log("Started backfill loop...");
    62.         Debug.Log(NeedsPlayers() ? "Needs players" : "Doesn't need players");
    63.  
    64.         while (NeedsPlayers())
    65.         {
    66.             localBackfillTicket = await MatchmakerService.Instance.ApproveBackfillTicketAsync(localBackfillTicket.Id);
    67.             if (!NeedsPlayers())
    68.             {
    69.                 Debug.Log("Stopping backfill...");
    70.                 await MatchmakerService.Instance.DeleteBackfillTicketAsync(localBackfillTicket.Id);
    71.                 localBackfillTicket.Id = null;
    72.                 Backfilling = false;
    73.                 return;
    74.             }
    75.             await Task.Delay(ticketCheckMs);
    76.         }
    77.         Backfilling = false;
    78.     }
    79.  
    80.     private async void NetworkManager_OnClientDisconnected(ulong clientId)
    81.     {
    82.         await MatchmakerService.Instance.UpdateBackfillTicketAsync(localBackfillTicket.Id, localBackfillTicket);
    83.  
    84.  
    85.         if (!Backfilling && NetworkManager.Singleton.ConnectedClients.Count > 0 && NeedsPlayers())
    86.         {
    87. #pragma warning disable 4014
    88.             BeginBackfilling();
    89. #pragma warning restore 4014
    90.         }
    91.     }
    92.  
    93.     private bool NeedsPlayers()
    94.     {
    95.         return NetworkManager.Singleton.ConnectedClients.Count < maxPlayers;
    96.     }
    97.  
    98.     public void Dispose()
    99.     {
    100.         NetworkManager.Singleton.OnClientDisconnectCallback -= NetworkManager_OnClientDisconnected;
    101.     }
    102. }