State machines
A State Machine lists state names; the scripts on its node hold the behaviour. For a state Run it calls, when they exist:
| Hook | When |
|---|---|
EnterRun() |
the machine enters Run |
ExitRun() |
the machine leaves Run |
FixedRun(dt float) |
every fixed step while in Run, before Motion 2D moves |
UpdateRun(dt float) |
every frame while in Run, after the scripts' Update |
func UpdateIdle(dt float) {
if node.Get<Motion2D>().Velocity.X != 0 {
node.Get<StateMachine>().Go("Run")
}
}
func EnterRun() {
node.Get<SpriteRenderer>().Tint = RunColor
}
Go(state) only queues the change: after the running hook returns, Exit of the old state and Enter of the new one run, then Changed(from, to) fires with the state indices. A Go inside Enter or Exit runs after that transition, so EnterRun may send the machine straight on to Air; at most eight changes happen in a row. Is(state) and TimeIn() read the state; Current, Previous and Time show it in the Inspector.
The machine enters Initial in the first frame, after the scripts' Ready. Hooks are found by name and found again after a hot reload, so renaming a hook takes effect at once. Lifecycle methods, event handlers and coroutines are never hooks. Problems warns about a machine without states, a state listed twice or an Initial that is not one of the states (GMP005).
Walk, stop and jump in the scene: the colour and the label follow the state.
Try it
Change the example parameters and compare the result. Reset restores the initial scene.
Scene6 nodes
Scriptmain.aes
package demo
import (
"aego/debug"
"aego/input"
)
// Player keeps its states in a State Machine: Idle, Run and Air, each with its own hooks and colour.
script Player {
func Update(dt float) {
m := node.Get<Motion2D>()
x := 0.0
if input.Down(input.KeyA) || input.Down(input.KeyLeft) {
x -= 1
}
if input.Down(input.KeyD) || input.Down(input.KeyRight) {
x += 1
}
m.Move(x)
if input.KeyPressed(input.KeySpace) || input.KeyPressed(input.KeyW) || input.KeyPressed(input.KeyUp) {
m.Jump()
}
if input.KeyReleased(input.KeySpace) || input.KeyReleased(input.KeyW) || input.KeyReleased(input.KeyUp) {
m.JumpReleased()
}
sm := node.Get<StateMachine>()
p := node.WorldPosition()
debug.Text(Vec2{p.X - 0.4, p.Y - 1}, sm.Current, Color{1, 1, 1, 1})
}
func UpdateIdle(dt float) {
m := node.Get<Motion2D>()
if !m.Grounded {
node.Get<StateMachine>().Go("Air")
} else if m.Velocity.X > 0.5 || m.Velocity.X < -0.5 {
node.Get<StateMachine>().Go("Run")
}
}
func UpdateRun(dt float) {
m := node.Get<Motion2D>()
if !m.Grounded {
node.Get<StateMachine>().Go("Air")
} else if m.Velocity.X < 0.5 && m.Velocity.X > -0.5 {
node.Get<StateMachine>().Go("Idle")
}
}
func UpdateAir(dt float) {
if node.Get<Motion2D>().Grounded {
node.Get<StateMachine>().Go("Idle")
}
}
func EnterIdle() {
node.Get<SpriteRenderer>().Tint = Color{0.96, 0.62, 0.04, 1}
}
func EnterRun() {
node.Get<SpriteRenderer>().Tint = Color{0.2, 0.8, 0.4, 1}
}
func EnterAir() {
node.Get<SpriteRenderer>().Tint = Color{0.3, 0.6, 1, 1}
}
}