FSM08 / 13

Scene states and the FSM

The current state determines which events are handled and which commands run.

SceneFSM holds the current state and the transition rules. FSMConfig contains the initial state, the states map, entry commands, and event handlers under on.

Here is an excerpt from the game configuration. fsm.config.ts

playing: {
  on: {
    [BlockPuzzleEvents.PlacementRequested]: {
      target: 'resolvingPlacement',
      actions: ['PlaceBlockCommand'],
    },
    [BlockPuzzleEvents.RestartRequested]: {
      actions: ['RestartBlockPuzzleCommand'],
    },
  },
},
01
playingAccepts PlacementRequested
02
resolvingPlacementWaits for the result event
03
checkingMovesRuns CheckBlockPuzzleMovesCommand on entry
04
playing or endedMovesAvailable continues the game. GameOver ends it.
This is the path for a successful move. A rejected placement returns from resolvingPlacement to playing.

The FSM ignores events absent from the current state’s on handlers. A rule with actions and no target runs the commands and keeps the current state. RestartRequested works this way.

Transition order

The SDK runs the actions in order. It then switches to the target and runs the new state’s entry commands. So while PlaceBlockCommand runs, the current state is still playing.

Events sent by a command wait in the queue. For example, the FSM handles PlacementCompleted after it changes state.

Sources ↗

Block Puzzle · fsm.config.ts↗States in the original game.