Documentation

Cloud Code

Turn-based game workflow

Follow this workflow to build a turn-based multiplayer game with CloudBehaviours.
Read time 2 minutesLast updated 15 days ago

Important
This page describes an experimental feature that might change significantly before release. It's not recommended to use or rely on experimental features in production environments due to potential instability.
A
MultiplayerSession
-scoped CloudBehaviour is a natural fit for a turn-based game: one instance holds the shared match state, and every player in the session sees it update. This workflow builds on a
MultiplayerSession
-scoped CloudBehaviour to add turn notifications, a turn clock, and hidden information.
To build a turn-based game with CloudBehaviours, complete the following tasks:

Prerequisites

Before you start, complete Get started with CloudBehaviours with a
MultiplayerSession
-scoped CloudBehaviour.

Notify players when a turn changes

Declare a public event on your cloud class, and invoke it when a turn ends. Cloud Code pushes the event to every client in the session, so each player's game reacts the moment a turn changes, without polling. Refer to Push channels in CloudBehaviours.

Time out an unresponsive player

Schedule a server-authoritative timer when a turn starts, so a player who closes the game can't stall the match. Mark the timer's target endpoint
[CloudCodeAccess(Access.Service)]
, so no player can call it directly. The timer can still call it. Refer to Timers in CloudBehaviours and Access control in CloudBehaviours.
Note
A timer callback runs with no player identity. If your timeout logic needs to know which player it's timing out, pass the player ID as an argument to the scheduled endpoint, rather than reading it from the calling context.

Show each player only their own hidden information

For a game with hidden information, such as a card game, define a per-player view of the state each player is allowed to see. Cloud Code builds and keeps each player's view in sync automatically. Refer to Per-player state views in CloudBehaviours.

Additional resources