Skip to content

Sub-states

One state is not a machine. The sprite needs to be waiting, or walking, or spinning, and those have to take turns.

They could be three states side by side, but then whatever has to be true all the time would have to be repeated in each of them. So instead they go inside Playing, as sub-states.

Click New sub-state on the Playing frame header three times. Each gets its own frame inside Playing, with its own entry node and its own four cells.

Three sub-states nested inside Playing.

Name them Idle, Aim and Walk from each one’s State settings, then give Idle the Start flag: the machine should rest there.

Naming the three sub-states and marking Idle as the start.

Exactly one sub-state carries the flag among its siblings, and that is the one the parent drops into when it is entered. Idle itself stays empty.

A state with sub-states runs both: its own cells, and the cells of whichever sub-state is currently active. The parent’s Every Frame runs every frame no matter which child is on.

So the shape to reach for is: what is always true goes on the parent, what takes turns goes on the children. A player script ends up as Alive on the outside, with Idle, Running and Jumping inside it, and the “did I die” check on the outside, written once.

Nesting goes as deep as you like.

The other way to nest is Move to on a state that already exists: it asks which state to move this one into, and the canvas redraws with it inside.

Move to, sending a state inside another one.

It is worth knowing because a machine rarely comes out right the first time. You build three states side by side, notice that two of them share a condition, and move them under a new parent. Nothing breaks: transitions point at states by id, so moving and renaming never break an arrow.