Tutorial


πŸ“˜ Godot State Machine Blueprint

A ready-to-use, modular finite state machine (FSM) template for 2D Godot projects. This example features two states: Idle and Walk. This blueprint is designed for rapid setup: simply follow the structure shown here, and you won’t need to modify any FSM code. Just add your own nodes, animations, collision shapes, and set the default state in the Inspector.
Perfect for player, enemy, or NPC logic in any 2D game.

❓ How to use

  1. Copy the statemachine/ folder into your project.
  2. Add a player folder with your scripts (e.g., player.gd, sprite_controller.gd, idle_state.gd, walk_state.gd)
  3. Set up your character scene:
    • Add a root node (e.g., CharacterBody2D)
    • Add a StateMachine node and attach game_state_manager.gd
    • Add state nodes (e.g., Idle, Walk) as children of StateMachine, each extending StateNode and implementing your logic.
    • Add a SpriteController node with three AnimatedSprite2D children (DownAnimatedSprite, UpAnimatedSprite, SideAnimatedSprite) for handling animations. (Assign animations to each AnimatedSprite2D using your assets)
    • Add a CollisionShape2D to your character for physics.
  4. Set the default_state property in the Inspector to your starting state. (e.g., Idle)


    Inspector view in Godot
    Inspector view in Godot

βš™οΈ Node Structure

  • Character: Main entity node (e.g., player).
  • StateMachine: Manages state transitions and logic.
  • Idle/Walk: Example state nodes, each with their own logic.
  • SpriteController: Handles animation and direction.

Node structure diagram


πŸ—‚οΈ Folder Structure

  • statemachine/: Contains generic, reusable FSM scripts. 
  • player/: Contains player-specific scripts and states.

Folder structure diagram


πŸ“œ Base Scripts

state_node.gd

This is the base class for all states. It defines the interface that all state scripts should implement.

game_state_manager.gd

Manages transitions between different states (such as Idle, Walk, etc.) for any character or game object. Each state is implemented as a child node (extending StateNode), and the manager automatically handles entering and exiting states, as well as calling transition logic each frame.

sprite_controller.gd

Handles sprite animation and direction for the character.

player.gd

Handles player movement, input, and updates the sprite controller.

Files

finite-state-machine.zip 3.2 MB
5 days ago
frog-idle.png 3.7 kB
5 days ago
frog-walk.png 4.2 kB
5 days ago

Get πŸ“˜ Godot State Machine Blueprint + Barista Frog

Buy Now$1.00 USD or more

Leave a comment

Log in with itch.io to leave a comment.