Skip to content

Tutorials · Animation and Cutscenes · Chapter 2

A state machine

15 minutes Uses: StateMachine

By the end

  • art/knight.asm: a graph with the states idle, run, land and air, and rise and fall inside air
  • Ten transitions in order, each with its condition and its blend
  • A State Machine on the knight, so that he runs, jumps, falls and lands with no field of his to set
  • The graph rehearsed in the Graph tab, and played
On this page

A graph for the knight

The knight has nine clips and nothing that chooses between them but a name you type. A State Machine chooses for him. It holds a graph: a box for each thing he can be doing, called a state, each naming the clip to play, and arrows between them that say when he stops doing one and starts another. The graph is a file of its own, a .asm, that you build in the Graph tab.

  1. Select knight, click Add component and choose StateMachineComponent. Its Graph is empty.
  2. Click the Graph tab, in the row with Viewport and Sprite sheet at the top of the window. It says there is no graph yet.
  3. Click New state machine. It writes art/knight.asm beside art/knight.anim, with one state, idle, that plays the clip idle, and puts it in the State Machine’s Graph.
The Graph tab on a knight with no graph yet: the node’s name, knight, at the top left, and in the middle of the empty tab the words No graph yet, a line saying a new machine starts with one state, idle, and nothing else, and a New state machine button
The Graph tab on a State Machine with no graph.
The knight selected, its Inspector narrowed with the search box, statemachine, to its State Machine: Graph knight.asm; the Viewport looks through the game camera at the hall with the knight at the left, and the Animation tab sits below
The State Machine on the knight. Its one field is the file.
Graph
The State Machine’s Graph: the .asm file that decides which clip the node’s Animated Sprite plays. Here art/knight.asm. Empty, the State Machine does nothing, and the Graph tab offers to make one. A graph names clips of the animation beside it, so knight.asm goes next to knight.anim and works for every node that plays that animation.

States, and a state inside a state

The knight does four things for now: he stands, runs, is in the air, and lands. Slashing and being hurt come in chapter 5. The graph gets a state for each of the four. The air is two clips, a rise and a fall, so air is a sub-graph: a state that is a graph of its own, with its own states, entry and arrows.

  1. In the row under the Graph tab’s bar, type run into the State box and run into the Clip box, and press Add state. A box called run appears, with its clip written under its name. (Left empty, Clip takes the state’s own name, which is the same here.)
  2. Add land the same way, with the clip land.
  3. Type air into the State box, leave Clip empty, and press Add sub-graph. The tab opens the new graph, one level down: the bar above the canvas reads knight / air.
  4. Add rise, with the clip rise, and fall, with the clip fall. If the bar says Entry is not rise, select rise and press Set entry.
  5. Click knight in the bar above the canvas to go back up. A new box lands on the first free place of a grid; drag the boxes where you like. In the picture, idle and run are on top and air and land under them.
The Graph tab on the knight’s graph: a dashed Any State box at the top left, and under it idle, the entry, selected, with the rehearsal’s green dot by its name, with run to its right, then air, a sub-graph of two states, and land to the right of it; arrows join them with their conditions written on them, such as speed greater than 10 and grounded is false; at the right the Parameters section lists grounded, speed, velocity_x and velocity_y, each marked the body, and the Rehearse section under it
The knight’s graph: four states, one of them a sub-graph, and the arrows between them.
The Graph tab one level down, in air: the bar above the canvas reads knight, then air, then the words Exit hands control back to the level above; a rise state and a fall state side by side with an arrow from rise to fall marked velocity_y greater than 0, and two arrows running down to a dashed Exit box, each marked grounded is true
Inside air: rise, fall and Exit.
State and Clip
A state is a box with a name and the name of the clip it plays. While the knight is in it, his Animated Sprite plays that clip. The two boxes in the bar make a state; the name must be new, and the clip is one of the animation’s own, run for run. A state’s clip can be changed afterwards by selecting it and choosing another clip’s chip.
Entry
The state the graph starts in. The bar names it, Entry: idle, and Set entry on any other state moves it. Every graph and every sub-graph has one: air’s is rise, so the knight enters it rising.
Sub-graph
A state that holds a graph. Double-click its box, or press Open, to look inside; the bar shows where you are, and clicking a name in it goes up. Inside, a state called Exit ends the sub-graph and hands control back to the level above, which decides what comes next.
Any State
The dashed box at the top left of a graph. It is not a state: an arrow from it can fire from every state but the one it enters. Nothing in the knight’s graph uses it yet. Chapter 5 does.

What his body tells the graph

A graph decides from numbers and switches, which it calls parameters. The knight has a Character Body, and a Character Body on a node with a State Machine writes four of them every step, before the graph reads them. The Graph tab lists them at the top right, marked the body.

ParameterKindWhat the body writes
groundedswitchon while he stands on something, off in the air
speednumberhow fast he is moving sideways, in units a second, never below 0
velocity_xnumberhis sideways velocity, negative to the left
velocity_ynumberhis vertical velocity, negative going up and positive coming down

The arrows, in order

An arrow is a transition: it leaves one state, enters another, and fires when its condition holds. The graph looks at its arrows in a fixed order, from the top of the Transitions list, and takes the first whose condition holds. Order is priority, and a new arrow is added at the end of the list.

  1. Select idle. In its band over the Transitions list, press Link, then click air: the row under the bar says Linking from idle — click a state, and the click ends it. A row idle to air appears in the list, with the condition Always.
  2. In that row, click Is. Type grounded into the box, and untick the checkbox, so it reads false: the arrow now fires when he is not on the ground.
  3. Add the rest of the root’s arrows the same way, in this order, with the condition and the blend of the table below. A blend is the box with an s after its number, in the row’s head: click it once, which selects its number, type the new one and press Enter.
  4. Open air. Add its three arrows in order, rise to fall, rise to the dashed Exit, and fall to Exit. Go back up to knight.
  5. Press Save in the Graph tab’s bar, and Ctrl+S.
OrderGraphFromToConditionBlend
0knightidleairgrounded Is false0
1runairgrounded Is false0
2idlerunspeed > 100.1
3runidlespeed < 100.15
4airlandFinished0
5landrunspeed > 100.1
6landidleFinished0.1
0airrisefallvelocity_y > 00
1riseExitgrounded Is true0
2fallExitgrounded Is true0
The Transitions list for the knight’s graph, headed First match wins: seven numbered rows, 0 to 6, each with arrows to move it up and down, its name, such as idle to air or land to idle, its blend in seconds, a bin, and a line of condition choices, Always, Finished, greater than, less than, Is and Trigger, with the chosen one lit and its parameter and value beside it
The root’s seven arrows, in order. The numbers are their priority.
Condition
The test that decides whether an arrow fires, chosen in the row. Always fires at once. Finished fires when the state’s clip has run out, or, for a state that holds a graph, when that graph has reached Exit. > and < compare a number parameter with a value you type. Is compares a switch with on or off. Trigger fires on a one-shot the game raises, which chapter 5 uses.
Blend
How long the cross-fade lasts, in seconds: the old state’s clip fades out while the new one fades in. 0 is a cut. About 0.1 to 0.15 suits two poses that are close, as idle and run are, and a longer one blurs a quick move. A blend never needs more than a few steps of the game.
First match wins
The Transitions list is the priority. The graph tries the arrows leaving the state it is in, top to bottom, and takes the first whose condition holds. The up and down arrows in a row move it, and the number is its place.

Rehearse it

The Graph tab can run a graph without the game. The Rehearse section under the parameters has a slider or a box for each, and the graph reads them as the game would.

  1. In Rehearse, drag the speed slider to the right, past 10.
  2. Press Step. The graph takes the first arrow that fires from idle: the section says In: run, and Took: idle to run, and the arrow lights green.
  3. Press Restart to go back to idle. The values you set stay.

The green dot by a state’s name is where the rehearsal stands. It does not follow the game: in Play, watch Clip in the Inspector.

The Graph tab after a rehearsal: speed dragged to 20.0 in the Rehearse section and Step pressed, the run state marked with a green dot and the arrow from idle to run lit green, and under the Step and Restart buttons the lines In run, Nothing fires from run and Took idle to run
speed at 20, one Step: the graph has taken idle to run.
Step and Restart
Rehearse’s two buttons. Step takes whichever arrow fires from the state the rehearsal is in, spending any trigger it read, and says where it ended. Restart puts the rehearsal back in the entry state and leaves the numbers as you set them.
Clip finished
A checkbox in Rehearse, for the one condition no parameter stands in for: whether the state’s clip has run out. Tick it and press Step to take a Finished arrow.

He plays it

  1. Select knight, and press F5.
  2. Hold D. The Inspector’s Clip changes from idle to run by itself. Let go, and it goes back.
  3. Press Space. It reads rise, then fall, then land, then idle.
  4. Press F5 again to stop. Clip is idle again in your project.
The editor playing the hall: the toolbar shows Stop, Pause, Step and Keep, the Viewport looks through the game camera with the knight a little to the right of where he began, and the Inspector, narrowed to the Animated Sprite, shows Clip run with the Animation tab below it open on the run clip
In Play, a moment after D goes down: the graph has chosen run and written it into Clip.

The graph, as this chapter leaves it

WhereWhatValue
knightState Machine Graphknight.asm
knight.asmentryidle
statesidle (idle), run (run), land (land), air (a sub-graph)
airentryrise
statesrise (rise), fall (fall)
transitionsas the table above, in its order

The boxes’ places on the canvas are yours: they change nothing but how the graph reads.

Check it works

Seven pictures of the knight side by side, each a step of the game after the one before: the first is the knight standing in his idle pose, and in the next five a fainter copy of the run, with the cape streaming out behind him, grows stronger over him as the idle fades, until the last is the run alone, the knight in mid-stride with the cape fully out
The cross-fade from idle to run, one step at a time: steps 30 to 36 (0.5 s to 0.6 s), a tenth of a second. D goes down at step 30, the graph takes idle to run and the blend shows from step 31, and it is whole at step 36.
Six pictures of the knight jumping, side by side: rising in a leap with his cape streaming behind, near the top of the jump with his cape bunched above him, falling with the cape flaring up, still falling close to the ground, crouched on the boards as he lands, and standing again
A jump, in six frames: steps 36 (0.6 s, rise), 48 (0.8 s, the top of the rise), 56 and 68 (0.93 s and 1.13 s, fall), 72 (1.2 s, land) and 90 (1.5 s, idle). Space goes down at step 30 (0.5 s).