Integrate matchmaking (Godot)
Overview
Matchmaking is the process by which two or more players can be evaluated for a match, and joined to a game session so they can play together. A player starts matchmaking by creating a match ticket in a match pool. The service evaluates the tickets in the pool with the pool's match ruleset and match function. When it finds a valid match, it creates a game session from the pool's session template and adds the matched players to it.
This article will show you how to integrate AGS Matchmaking into your Godot game client using the Godot SDK.
To learn more about each component, see the overview of the matchmaking flow.
Goals
This article aims to provide you an understanding of the matchmaking player experience flow and show you how to integrate:
- Starting matchmaking
- Waiting for a match result
- Canceling matchmaking before a match is found
- Connecting to the game host after the match
Prerequisites
To complete all the steps in this article, you will need:
- A familiarity with AGS Session and Matchmaking.
- A basic understanding of Godot.
- The AGS Game SDK installed into your Godot project and set up using its required client ID. See Install the Godot SDK.
- Access to the AGS Admin Portal.
- A session template, match ruleset, and match pool created and configured.
- Your game client to have authenticated users with the AGS backend. See Implement login with device ID.
Choose your host connection path
Before writing the matchmaking code, choose how matched players will connect after the match is found:
| Hosting model | What to configure before this guide | What to do after a match is found |
|---|---|---|
| AMS dedicated server | A session template that requests an AMS dedicated server. | Wait for the server address in the game session, then connect to the server with AccelByteGameTransport. |
| P2P | A P2P session template. See Set up P2P transport using the Godot SDK. | Compare the leader ID with the current user ID. The leader hosts and the other players join the leader. |
Implementing the matchmaking flow
In the following snippets, sdk is an AccelByteSDK instance that is already logged in. Calls return a Dictionary with success, status_code, data and error. Pass an empty namespace to use the namespace set in the SDK configuration.
Start matchmaking
To start matchmaking, create a match ticket in a match pool.
func start_matchmaking(match_pool: String) -> String:
var match2: Match2Service = sdk.get_match2_service()
var body: Dictionary = {
"matchPool": match_pool,
"attributes": {},
# The latency in milliseconds to each region. A dedicated server pool needs it.
"latencies": {"us-west-2": 20, "us-east-1": 40},
}
var result: Dictionary = await match2.create_match_ticket("", body)
if result.get("success", false):
# Do something if CreateMatchmakingTicket succeeds
var match_ticket_id: String = result.get("data", {}).get("matchTicketID", "")
print("Match ticket created: ", match_ticket_id)
return match_ticket_id
# Do something if CreateMatchmakingTicket fails
print("Error CreateMatchmakingTicket: ", result.get("error", ""))
return ""
Additional parameters to start matchmaking
You can add more information to the match ticket body:
| Parameter | Description |
|---|---|
attributes | Custom attributes that the match ruleset can use, for example {"map_names": "map_01"}. |
latencies | The latency in milliseconds to each region, for example {"us-west-2": 20}. |
excludedSessions | The IDs of game sessions that the player doesn't want to be matched into again. See Record and exclude sessions from matchmaking. |
sessionID | The ID of the party session, when a party starts matchmaking together. |
storage | Custom data that is stored with the ticket. |
Wait for a match
After you create the ticket, check its status until a match is found. When matchFound is true, the ticket status has the ID of the game session that the players were put into.
func wait_for_match(match_ticket_id: String, timeout_sec: float = 120.0) -> String:
var match2: Match2Service = sdk.get_match2_service()
var waited: float = 0.0
while waited < timeout_sec:
var result: Dictionary = await match2.match_ticket_details("", match_ticket_id)
var status: Dictionary = result.get("data", {})
if status.get("matchFound", false):
# Do something if a match is found
return status.get("sessionID", "")
await get_tree().create_timer(2.0).timeout
waited += 2.0
# Do something if no match was found in time
return ""
Keep the ticket ID from the previous step. If the player cancels or leaves, delete the ticket as described in Cancel matchmaking.
Join a game session
When a match is found, the matched players are added to the game session if the session template has auto join enabled. If it doesn't, the player receives a game session invitation and has to join the session. See Integrate game session to learn how to listen to invitations and join a session.
To check the members of the matched session, get the game session details:
func get_matched_session(session_id: String) -> Dictionary:
var session: SessionService = sdk.get_session_service()
var result: Dictionary = await session.get_game_session("", session_id)
if result.get("success", false):
# Do something if GetGameSessionDetails succeeds
return result.get("data", {})
# Do something if GetGameSessionDetails fails
print("Error GetGameSessionDetails: ", result.get("error", ""))
return {}
Connect to the game host
- AMS dedicated server
- P2P
An AMS dedicated server is claimed for the session after the match is found, and it takes a moment to publish its address. AccelByteGameTransport waits until the game session has the server address, and then it connects to the server with ENet.
func connect_to_dedicated_server(session_id: String) -> void:
var transport := AccelByteGameTransport.new()
transport.initialize(sdk)
# Waits until the session reports the server address. Allow tens of seconds.
var peer: ENetMultiplayerPeer = await transport.connect_to_ds(session_id, 45.0)
if peer == null:
# Do something if the connection failed
push_error(transport.last_error)
return
# Do something if the connection is created
multiplayer.multiplayer_peer = peer
multiplayer.connected_to_server fires when the connection is up, and multiplayer.connection_failed fires when it isn't. To set up the dedicated server itself, see Integrate dedicated servers with the SDK.
ENet doesn't work in browsers, so a web export can't connect to a dedicated server with AccelByteGameTransport. Use P2P for web games.
For a P2P session, follow Set up P2P transport using the Godot SDK. The session leader hosts, and the other players join the leader.
Cancel matchmaking
If you need to cancel matchmaking before the process completes or timeout occurs, delete the match ticket:
func cancel_matchmaking(match_ticket_id: String) -> void:
var match2: Match2Service = sdk.get_match2_service()
var result: Dictionary = await match2.delete_match_ticket("", match_ticket_id)
if result.get("success", false):
# Do something if DeleteMatchmakingTicket succeeds
print("Match ticket deleted")
else:
# Do something if DeleteMatchmakingTicket fails
print("Error DeleteMatchmakingTicket: ", result.get("error", ""))
Troubleshoot issues
No match is found
If a ticket stays in the pool until it times out, the player is removed from matchmaking. This often happens if the timeout defined in the match pool configuration is too short or the match ruleset doesn't allow enough rule flexing to enable the player to match with others. To resolve:
- Review the match pool configuration to determine if the match or backfill ticket timeout is too short.
- Review the match ruleset to make sure the minimum player or team requirements have been set correctly.
connect_to_ds returns null
The game session has no server address yet. Check that the session template type and the claim keys match your fleet, that the dedicated server sent ready, and that a server was available. AccelByteGameTransport.last_error has the reason.