Skip to main content
Coding Quests

Godot State Machine

Enemy code turns into a pile of if statements once it can do more than two things. A state machine gives each behaviour its own small script and one clear way to switch between them.

Free Godot 4.7 project · MIT licensed · v1.0.0

No account needed. Or download the v1.0.0 ZIP directly.

Slimes switching between Roam, Idle, Chase, Attack, Hitstun and Death, with a log of every change.

What is in it

  • Two small scripts: a StateMachine of about 70 lines and a State of about 30.
  • One node per state. Add a child node and attach a script to it.
  • States leave by emitting a signal, and outside code can force a change, for example on a hit.
  • Four hooks per state: enter, exit, an update every frame and an update every physics frame.
  • A state_changed signal for debug labels and logs.
  • A complete slime enemy with six states and pathfinding with NavigationAgent2D, Godot's node that finds a route around obstacles.

Quick start

Download the project from GitHub and unzip it. In the Godot Project Manager, click Import and pick the project.godot file inside the folder.

  1. Press F5.
  2. Move with WASD and swing with Space or a left click.
  3. Walk toward a slime and watch its label change to Chase, then Attack.
  4. Dodge the leap, then hit the slime while it is pale blue for double damage.

The README on GitHub explains how each part works and how to copy it into your own game.

Compatibility and limits

Made for
Godot 4.7, standard build. GDScript only, no .NET needed.
Tested on
Godot 4.7.2 stable, on Windows. Earlier versions of Godot 4 are untested.
Renderer
Compatibility
Code license
MIT. You can use it in commercial games. Read the license
Art and fonts
Knight, sword and slime sprites from Kenney's Tiny Dungeon pack (CC0 1.0).
Release
v1.0.0, commit 1dc8de0, published 2026-10-03

Known limits

  • One state at a time. There are no nested or parallel states.
  • Transitions use node names, so a typo in a name is silently ignored.
  • The demo room is open. Levels with walls need a real navigation polygon.

Learn to build it

Copying a system works until you need to change it. These quests build it with you, one small step at a time. You write the GDScript in your browser and each step is checked before you move on.

2D Top-Down Enemy AI

Builds this six-state machine: idle, roam, chase, the telegraphed leap, hitstun and death, with pathfinding.

First 2 lessons free

State Machine AI for 3D Enemies

Takes the same idea into 3D.

First 4 lessons free

See all the free Godot projectsReport a problem with this project