Files
godot-demo-projects/2d/finite_state_machine
Hugo Locurcio 7befd7c718 Enable physics interpolation in all 2D demos (#1070)
This makes uses of the new built-in 2D physics interpolation
added in 4.3.

For 3D demos, a separate PR requiring `master` will be made later.
2025-04-14 18:58:38 +02:00
..
2025-03-21 02:00:41 +01:00
2025-03-21 02:00:41 +01:00
2025-03-21 02:00:41 +01:00

Hierarchical Finite State Machine

This example shows how to apply the State machine programming pattern in GDscript, including Hierarchical States, and a pushdown automaton.

Language: GDScript

Renderer: Compatibility

Check out this demo on the asset library: https://godotengine.org/asset-library/asset/2714

Why use a state machine

States are common in games. You can use the pattern to:

  1. Separate each behavior and transitions between behaviors, thus make scripts shorter and easier to manage.

  2. Respect the Single Responsibility Principle. Each State object represents one action.

  3. Improve your code's structure. Look at the scene tree and FileSystem tab: without looking at the code, you'll know what the Player can or cannot do.

You can read more about States in the excellent Game Programming Patterns ebook.

Screenshots

Screenshot