# Match players for online play with Game Center

> Tell an Agent how players of your game should be matched online. It sets up a Game Center matchmaking queue and rules, tests them with made-up players, and connects the game to them.

Source: https://air.build/docs/app-store/match-players-for-multiplayer/

When a player starts an online game, Game Center finds other players for them. Matchmaking rules let you decide who plays whom, for example players who picked the same mode or have a similar skill. Apple's GameKit article explains the setup. Rules are grouped into a rule set and the rule set is attached to a queue. The game names that queue when it asks for a match, and Game Center uses the rules to find the best matches. The article describes this for real-time games, where players play at the same time. See [Finding players using matchmaking rules](https://developer.apple.com/documentation/gamekit/finding-players-using-matchmaking-rules).

Rules are optional. Apple's article on backward compatibility says that match requests that don't name a queue go to a default queue, which Apple calls classic matchmaking. You need rules only if you want a say in who plays whom.

> [!NOTE]
> AirBuild has no Game Center screen. You ask an Agent in the Project's chat, and it uses `asc game-center matchmaking` (listed under `game-center` in [asc: apps and App Store listing](https://air.build/docs/cli/asc-apps/#game-center)). To see the result, ask the Agent.

## Before you start

- The game's own code must use Game Center matchmaking. A queue and its rules do nothing until the game sends match requests to it.
- The game needs Game Center switched on, an App Store Connect record and your Apple Developer account. See [Add Game Center leaderboards and achievements](https://air.build/docs/app-store/add-game-center-leaderboards/). An install signed with a free Apple ID leaves Game Center out, so online play won't work in it.

If your game has no online play yet, ask your Agents to add it first, for example:

```text
Add online races against up to 3 other players, using Game Center matchmaking to find them.
```

## Describe how players should be matched

Say in the Project's chat how many people play together and what decides who plays whom, for example:

```text
Set up Game Center matchmaking for Fox Run: 2 to 4 players per race. Only match players who picked the same track, and put players with similar best scores together. If someone waits more than 20 seconds, allow a bigger score gap.
```

Rules can only use what the game sends with each match request. Describe things the game knows, such as the mode, track or level a player picked, or a score it keeps.

1. The Agent creates a rule set with the fewest and the most players per match, with `asc game-center matchmaking rule-sets create`.
2. It turns what you described into rules with `asc game-center matchmaking rules create`. Each rule has a kind, from the table below, and an expression that Game Center checks against match requests.
3. If players play in teams, it adds the teams with `asc game-center matchmaking teams create`, each with its fewest and most players.
4. It creates a queue for the rule set with `asc game-center matchmaking queues create`. The game's code finds the queue by its reference name.
5. It tests the rules with made-up players. See "Test the rules" below.
6. It changes the game's code so that each match request names the queue and sends what the rules check, such as the track and the best score.
7. It tells you in the chat what it created. To see it again at any time, ask "Which matchmaking queues and rules does the game have?". The Agent reads them with `asc game-center matchmaking queues list` and `asc game-center matchmaking rules list`.

Apple asks for queue and rule set names that are unique in your organization, such as `com.example.mygame.GameSettingsQueue`. `asc` lists queues and rule sets for the whole Apple team, not for one game, so names that include the game's name keep them apart.

### Kinds of rules

Apple's [Matchmaking rules](https://developer.apple.com/documentation/gamekit/matchmaking-rules) article describes four kinds. Game Center applies them in this order:

| Kind | What it checks | Apple's example |
| --- | --- | --- |
| `COMPATIBLE` | Whether two match requests can play together: yes or no. | The game version is 2.0 or later. |
| `DISTANCE` | How alike two requests are, from 0.0 (most alike) to 1.0 (least alike). The most alike are put together first. | Low network delay between players. |
| `MATCH` | Whether a possible match is allowed: yes or no. | A skill range between 0 and 5. |
| `TEAM` | Whether the teams of a possible match meet a condition: yes or no. | A total skill difference between teams of less than 1. |

To loosen a rule the longer players wait, the Agent writes it as a `MATCH` rule whose value changes with how long a request has been waiting. Apple's [Finding players using matchmaking rules](https://developer.apple.com/documentation/gamekit/finding-players-using-matchmaking-rules) describes how.

### What to decide

Say these in your request if you have a preference.

| Choice | What it is |
| --- | --- |
| Players per match | The fewest and the most. Apple's article says Game Center looks for matches with the most players first. |
| Who plays whom | What should be the same, or close, for players in one match. |
| Waiting | Whether the rules loosen the longer a player waits, and after how long. |
| Teams | How many teams, and the fewest and most players on each. |

## Test the rules

Before players use the rules, ask the Agent to try them, for example:

```text
Test the matchmaking rules with six made-up players: four on the forest track with close scores, and two on the desert track.
```

The Agent writes the made-up match requests to a file and runs `asc game-center matchmaking rule-set-tests create --file`. Apple's API reference says a rule set test applies the rule set to sample match requests. See [Test a Rule Set](https://developer.apple.com/documentation/appstoreconnectapi/post-v1-gamecentermatchmakingrulesettests). The answer shows which requests were put together, and on which team. The Agent tells you whether that's what you described.

To try a real match:

1. Have your Agents upload a build with the change. See [Share a beta with TestFlight](https://air.build/docs/app-store/test-with-testflight/#upload-a-new-build).
2. Install it from TestFlight on a device your game runs on, such as an iPhone, iPad or Mac, and invite enough testers to reach the fewest players per match. See [Share a beta with TestFlight](https://air.build/docs/app-store/test-with-testflight/#invite-testers).
3. Start online play at about the same time, each signed in to Game Center.

If players aren't put together the way you expected, tell the Agent who played, what each picked, and what happened.

## Ship it

- The queue, rule set and rules don't go to App Review. They aren't among the items `asc review items add` can put in a submission. Apple's article describes uploading rules to the Game Center server.
- The game's code that uses them reaches players with a new version. See [Ship an update to your app](https://air.build/docs/app-store/ship-an-update/). If this is the game's first update with Game Center, the version also needs Game Center switched on: see [Add Game Center leaderboards and achievements](https://air.build/docs/app-store/add-game-center-leaderboards/#ship-them).

## See how matching goes

Ask, for example:

```text
How many match requests found a match this week, and how many gave up or expired?
```

The Agent uses `asc game-center matchmaking metrics queue-requests`, which counts a queue's requests by result: `MATCHED`, `CANCELED` or `EXPIRED`. For a single rule, it can fetch rule metrics such as `asc game-center matchmaking metrics rule-errors`. If many requests expire, you can ask the Agent to loosen the rules.

## Change or remove the rules

Say what should change. The Agent uses:

| To | Command |
| --- | --- |
| Change the fewest or most players per match | `asc game-center matchmaking rule-sets update` |
| Change what a rule checks, or its description | `asc game-center matchmaking rules update` |
| Add or remove a rule | `asc game-center matchmaking rules create` or `delete` |
| Change how many players a team has | `asc game-center matchmaking teams update` |
| Use another rule set for the queue | `asc game-center matchmaking queues update --rule-set-id` |

- `rules update` can't change a rule's kind. For that, the Agent deletes the rule and creates a new one.
- `asc` can't rename a queue, and the game's code uses the queue's name.
- A rule that checks something new only works once the game sends it, which needs a new version of the game.
- Deleting a queue, rule set, rule or team needs `--confirm`, so the Agent asks you first. `asc` has no command to bring a deleted one back.

## Questions

**I set up rules, but players aren't matched by them.**

Game Center uses the rules only for match requests that name the queue. Ask the Agent to check that the game's code uses the queue's name and sends what the rules check, and make sure everyone is playing a build with that code.

**Can players on different versions of the game play together?**

Apple calls this multiplayer compatibility. You set it for each version of the game in App Store Connect on the web: `asc` can list it (`asc game-center app-versions compatibility list`) but not set it. See Apple's [Add multiplayer compatibility](https://developer.apple.com/help/app-store-connect/configure-game-center/add-multiplayer-compatibility).

**My game already had online play without rules. Can old and new versions play each other?**

Apple's article says Game Center doesn't match players on versions without rules against versions that name a queue, unless the queue lists the game's bundle ID for classic matchmaking. Ask the Agent to add it with `asc game-center matchmaking queues update --classic-bundle-ids`. See Apple's [Creating matchmaking rules for backward compatibility](https://developer.apple.com/documentation/gamekit/creating-matchmaking-rules-for-backward-compatibility).

**Can players of my two games be matched together?**

Apple's backward-compatibility article says a Game Center group shares matchmaking between games, along with leaderboards and achievements. See [Share leaderboards and achievements between your games](https://air.build/docs/app-store/share-game-center-between-your-games/), and tell the Agent which games should play each other.

**Does it work on older iPhones?**

Apple's GameKit reference lists the setting a match request uses to name a queue ([queueName](https://developer.apple.com/documentation/gamekit/gkmatchrequest/queuename)) from iOS and iPadOS 17.2. Ask your Agents what the game does for players on older versions.
