Game development

Stage 2 · Lesson 6 of 7

State machines

Programming for games

A treats an object as being in exactly one state at a time, such as idle, running, jumping, attacking or hurt. It also sets clear rules for moving from one state to another. This replaces a tangle of (true or false flags) like isJumping and isAttacking, which allow impossible combinations.

Start with an (a fixed list of named options) and a switch or match statement. Once the states grow, move to one class or node per state. Player controllers, enemy AI, menus and match flow all use state machines. Your engine's animation system is one too.

Common trapadding a new boolean for each new behaviour until the player can attack while dead or jump while in a menu.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

  • Free Godot 4 project tutorials and open source demos to build and read

  • A friendly one-sitting Godot course that builds a small platformer

Keep handy

Also worth your time

Mark this stage as not done?