The PlayFlow SDK includes a client for the PlayFlow Matchmaking service, which allows you to find matches for players based on skill, region, and custom data. This is primarily handled by the PlayFlowMatchmakerManager.

PlayFlowMatchmakerManager

This component, which you add to a GameObject in your scene, provides a simple async method for finding a match.

Finding a Match

To find a match, you create a MatchRequest object with your desired parameters and pass it to the FindMatchAsync method. This method will poll the backend until a match is found, the operation is canceled, or it times out.

using PlayFlow;
using System;
using System.Threading.Tasks;

public class MyMatchmaker : MonoBehaviour
{
    public PlayFlowMatchmakerManager matchmakerManager; // Assign in Inspector

    public async void StartMatchmaking()
    {
        var matchRequest = new PlayFlowMatchmakerManager.MatchRequest(
            "DefaultMatchmaker", // Your matchmaker config name from the dashboard
            "player-123"         // The unique ID of the player
        );

        // You can also add optional parameters
        // matchRequest.Elo = 1500;
        // matchRequest.Regions = new List<string> { "us-east" };

        try
        {
            // This will wait until a match is found or the timeout is reached
            JObject matchData = await matchmakerManager.FindMatchAsync(
                matchRequest, 
                TimeSpan.FromSeconds(60) // How long to wait for a match
            );
            
            Debug.Log("Match Found!");

            // The 'matchData' object contains the server details.
            // You can use the Lobby Manager to easily parse this.
            var connectionInfo = PlayFlowLobbyManagerV2.Instance.GetGameServerConnectionInfo();
            if (connectionInfo.HasValue)
            {
                Debug.Log($"Connect to server at {connectionInfo.Value.Ip}:{connectionInfo.Value.Port}");
                // Use this info to connect your game client (e.g., Netcode, Mirror, etc.)
            }
        }
        catch (TimeoutException)
        {
            Debug.LogWarning("Matchmaking timed out. No match was found in time.");
        }
        catch (OperationCanceledException)
        {
            Debug.Log("Matchmaking was canceled by the user.");
        }
        catch (Exception ex)
        {
            Debug.LogError($"An error occurred during matchmaking: {ex.Message}");
        }
    }
}

Understanding the Response

When a match is found, the matchData JObject returned by FindMatchAsync will contain all the information about the match, including the players involved and the details of the allocated game server.

The PlayFlowLobbyManagerV2 is aware of the match and its associated server, so you can conveniently use GetGameServerConnectionInfo() to get the IP and port needed to connect your game client.