Godot state machine advanced

From nix-community/nur-combined · View interactive version

Godot state machine advanced is an AI agent skill from the GitHub repository nix-community/nur-combined. Expert blueprint for hierarchical finite state machines (HSM) and pushdown automata for complex AI/character behaviors. Covers state stacks, sub-states, transition validation, and state context passing. Use when basic FSMs are insufficient OR implementing layered AI. Keywords state machine, HSM, hie

How to use this skill

Skill file

Advanced State Machines

Hierarchical states, state stacks, and context passing define complex behavior management.

Available Scripts

[hsm_hierarchical_base.gd](scripts/hsm_hierarchical_base.gd)

Advanced HSM base delegator for propagating physics and input to sub-states.

[hsm_pushdown_stack.gd](scripts/hsm_pushdown_stack.gd)

Professional Pushdown Automata for interruptive state (Pause/Menu) stacking.

[hsm_state_context.gd](scripts/hsm_state_context.gd)

Decoupled context object pattern for passing persistent data between states.

[hsm_transition_guard.gd](scripts/hsm_transition_guard.gd)

Expert transition validation logic to prevent illegal state changes.

[hsm_animation_syncer.gd](scripts/hsm_animation_syncer.gd)

Automated Logic-to-AnimationTree syncing with state-based travel logic.

[hsm_concurrent_logic.gd](scripts/hsm_concurrent_logic.gd)

Orchestration for parallel state machines (e.g., Move + Attack).

[hsm_resource_state_loader.gd](scripts/hsm_resource_state_loader.gd)

Data-driven state definition using custom Godot Resources (.tres).

[hsm_reentry_aware_state.gd](scripts/hsm_reentry_aware_state.gd)

Handling resume-from-stack logic vs fresh entry events.

[hsm_state_history_logger.gd](scripts/hsm_state_history_logger.gd)

Debug ring-buffer for tracking state transition history and stack depth.

[hsm_state_timer_component.gd](scripts/hsm_state_timer_component.gd)

Auto-transition component for finite states like Stun or Dash.

MANDATORY: Read hsm_logic_state.gd before implementing hierarchical AI behaviors.

NEVER Do (Expert State Rules)

Hierarchy & Delegation

Transitions & Lifecycle

Architecture & Context


# hierarchical_state.gd
class_name HierarchicalState
extends Node

signal transitioned(from_state: String, to_state: String)

var current_state: Node
var state_stack: Array[Node] = []

func  _ready() -> void:
    for child in get_children():
        child.state_machine = self
    
    if get_child_count() > 0:
        current_state = get_child(0)
        current_state.enter()

func transition_to(state_name: String) -> void:
    if not has_node(state_name):
        return
    
    var new_state := get_node(state_name)
    
    if current_state:
        current_state.exit()
    
    transitioned.emit(current_state.name if current_state else "", state_name)
    current_state = new_state
    current_state.enter()

func push_state(state_name: String) -> void:
    if current_state:
        state_stack.append(current_state)
        current_state.exit()
    
    transition_to(state_name)

func pop_state() -> void:
    if state_stack.is_empty():
        return
    
    var previous_state := state_stack.pop_back()
    transition_to(previous_state.name)

State Base Class

# state.gd
class_name State
extends Node

var state_machine: HierarchicalState

func enter() -> void:
    pass

func exit() -> void:
    pass

func update(delta: float) -> void:
    pass

func physics_update(delta: float) -> void:
    pass

func handle_input(event: InputEvent) -> void:
    pass

Best Practices

  1. Separation - One state per file
  2. Signals - Communicate state changes
  3. Stack - Use push/pop for interruptions

Reference

Related

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.