Turn-based game workflow
Follow this workflow to build a turn-based multiplayer game with CloudBehaviours.
Read time 2 minutesLast updated 15 days ago
A -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 -scoped CloudBehaviour to add turn notifications, a turn clock, and hidden information.
MultiplayerSessionMultiplayerSessionTo build a turn-based game with CloudBehaviours, complete the following tasks:
- Notify players when a turn changes
- Time out an unresponsive player
- Show each player only their own hidden information
Prerequisites
Before you start, complete Get started with CloudBehaviours with a -scoped CloudBehaviour.
MultiplayerSessionNotify 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 , so no player can call it directly. The timer can still call it. Refer to Timers in CloudBehaviours and Access control in CloudBehaviours.
[CloudCodeAccess(Access.Service)]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.