All examples · State and scripts · As Markdown, for AIs

States inside states

What this example shows

The guard of State machine, now calm or in combat, each with states of its own: calm patrols, searches and goes back; combat approaches, swings and recovers. Each inner machine is a useStateMachine given { parent: guard, state: 'calm' }: it runs only while the guard is in that state, and is left and entered with it. Arrow keys or A and D walk.

What holds for every move of a state is written once, in that state's onUpdate, which runs before the inner one. Calm looks out for the hero for all three of its moves; combat notices when he got away, and sends the guard back with calm.go('search'), which takes it to calm straight into searching rather than patrolling. Each machine has its own time, so the HUD shows how long the guard has been in combat and how long in this move of it.

Starting
Starting
—