Turn-based game workflow
Follow this workflow to create a turn-based multiplayer game with CloudBehaviours.
Read time 2 minutesLast updated 12 days ago
A -scoped CloudBehaviour suits a turn-based game. One instance holds the shared match state, and every player in the session receives its updates. Start from a -scoped CloudBehaviour, then add turn notifications, a turn clock, and hidden information.
MultiplayerSessionMultiplayerSessionTo create 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 the event when a turn ends. Each time the event fires, every client in the session receives a message, so each player's game reacts as soon as a turn changes, without polling. For how to declare a push channel and listen for it in your game, 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 hold up the match. Mark the endpoint that the timer calls with , so no player can call the endpoint directly. The timer can still call it. For how to schedule and cancel the timer, refer to Timers in CloudBehaviours. For how to restrict the endpoint, refer to 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 that each player can access. Cloud Code builds each player's view and keeps it up to date automatically. For how to define the view, refer to Per-player state views in CloudBehaviours.