Documentation

Multiplayer

Multiplayer

Turn-based quickstart

Install the turn-based quickstart sample to build a server-authoritative turn-based multiplayer game using Cloud Code.
Read time 4 minutesLast updated 2 days ago

Use the turn-based quickstart sample to build a server-authoritative turn-based game without running a dedicated game server. Use this sample as a pattern for games like chess, card, and word games, where players take turns and the game state must stay protected from cheating.
This quickstart uses a Cloud Code C# module and the Unity Multiplayer Services SDK to run the game logic on the server. Players join a session, and the session ID scopes the shared game state in Cloud Code. The module validates every move against the caller's authenticated player ID and sends the result to the other player as a push message.

Required components

The turn-based sample demonstrates how several components work together to keep game state authoritative and prevent cheating:
  • Cloud Code module: made up of classes that run in Cloud Code, not on any player's device.
  • Authentication package: signs each player in anonymously, so the module has a trusted player ID to check every call against.

Set up the project

  1. Install the turn-based sample using the Multiplayer Center.
  2. Select the cloud environment the
    TurnBasedSample
    Cloud Code module runs against in Project Settings > Services > Environments.
  3. Select the sample's pre-configured Play mode scenario,
    PlayerTurnBasedConnection
    , from the drop-down menu next to the Play button.
In the Unity Editor, you don't need to deploy the Cloud Code module before you test. When you enter Play mode, the scenario deploys the module to a local Cloud Code server for you. To deploy the module to your cloud environment for builds, refer to Deploy the Cloud Code module for builds.

Implement server-authoritative game logic with Cloud Code

The game logic lives entirely in a Cloud Code C# module, authored directly in the Unity project, in the sample's
Cloud
folder. The
TurnBasedSample.ccmu
asset links that module to the client code that calls it.
The module scopes state to the multiplayer session with
[StateScope(Scope.MultiplayerSession)]
, so its fields (the list of joined players, every move played, and whose turn comes next) persist between calls and both players can access them for as long as that session exists.
The module pushes three message types as the game progresses:
turnbased-game-started
,
turnbased-move-played
, and
turnbased-game-finished
. On the client, a generated
TurnBasedGameClient
gives you one typed method per module function, scoped to a session ID.

Connect players with quick join

The sample connects players through a quick-join session. The session carries no network connection: it's used only to bring the players together, and its ID is the scope for the Cloud Code module's state.
ConnectionManager
waits for
PlayerAuthentication
to sign the player in, then calls
MultiplayerService.Instance.MatchmakeSessionAsync
with a
QuickJoinOptions
value and a
SessionOptions
value that sets
MaxPlayers
to
PlayersPerGame
(2).
Because one player creates the session, there's no host to configure. Whichever player asks first creates the session, and the game itself is still owned by the Cloud Code module, not by that player.

Multiplayer services

Note
The Multiplayer Services SDK uses multiple Unity Gaming Services to provide its functionality, which contributes to the billing of those services.
This sample uses the Multiplayer Services SDK from both sides of a client/server split: the server-side API (
MultiplayerServerService
,
ServerAuthenticationService
) to create and authenticate the session, and the client-side API (
MultiplayerService
,
PlayerAuthentication
) to sign in anonymously and quick-join it. Refer to Create a session and Game server hosting support for background on both sides of this API.

Testing

When you enter Play mode with the
PlayerTurnBasedConnection
scenario active, the scenario deploys the
TurnBasedSample
module to the local Cloud Code server, opens the Example scene, and starts a second virtual player. Both players sign in. The main Editor quick-joins and creates a session, the virtual player joins it, and both players take turns.
The virtual player keeps looking for the main Editor's session for up to 30 seconds before it gives up. If the main Editor is slow to enter Play mode, increase Virtual Player Join Timeout Seconds on the
ConnectionManager
component.
In the Unity Editor, each player only matches sessions created from the same machine, so the two Play mode players find each other, and two people sharing an environment stay out of each other's games.

Deploy the Cloud Code module for builds

A build calls the module in your cloud environment, not the local Cloud Code server used in Play mode. To deploy the
TurnBasedSample
module to the environment you selected:
  1. Open the Play Mode Scenarios window (Window > Play Mode > Scenarios).
  2. Select the
    PlayerTurnBasedConnection
    scenario.
  3. Set its Cloud Code Deploy Target to Remote.
  4. Enter Play mode.

Modify the quickstart

You can use the turn-based sample as the basis for your own implementation. When imported via the Multiplayer Center, it's installed as an embedded package. Modify it directly, or copy assets out of the package into your own project to edit them there.

Additional resources