godot-state-machine-advanced
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdvanced State Machines
高级状态机
Hierarchical states, state stacks, and context passing define complex behavior management.
分层状态、状态栈和上下文传递定义了复杂行为的管理方式。
Available Scripts
可用脚本
hsm_logic_state.gd
hsm_logic_state.gd
Expert HSM base class with state stack management and validation.
具备状态栈管理与验证功能的专业HSM基类。
pushdown_automaton.gd
pushdown_automaton.gd
Stack-based state machine for interrupt-resume behavior (pause menus, cutscenes).
MANDATORY: Read hsm_logic_state.gd before implementing hierarchical AI behaviors.
基于栈的状态机,用于实现中断-恢复行为(如暂停菜单、过场动画)。
必须注意:在实现分层AI行为前,请先阅读hsm_logic_state.gd。
NEVER Do in Advanced State Machines
高级状态机中的绝对禁忌
- NEVER forget to call exit() before enter() — Transition without exit? Previous state cleanup skipped = resource leaks (timers, tweens). ALWAYS exit → enter.
- NEVER use push_state() without pop_state() — Pushed 100 interrupt states? Stack overflow + memory leak. EVERY push needs matching pop.
- NEVER modify state during transition — called inside
transition_to()? Re-entrant transition = undefined behavior. Flag transitions, execute after current completes.exit() - NEVER skip state validation — but state doesn't exist? Silent failure OR crash. Validate state exists before transition.
transition_to("AttackState") - NEVER forget to process child states — Hierarchical state with sub-states? Parent must call
update()for delegation.current_child_state.update() - NEVER use string state names in code — typo = silent failure. Use constants OR enums:
transition_to("Idel").transition_to(States.IDLE)
gdscript
undefined- 绝对不要在调用enter()之前忘记调用exit() —— 不执行exit就转换状态?会跳过之前状态的清理操作,导致资源泄漏(如计时器、补间动画)。必须始终遵循先exit再enter的顺序。
- 绝对不要只调用push_state()而不调用pop_state() —— 推入100个中断状态?会导致栈溢出和内存泄漏。每一次push都必须对应一次pop。
- 绝对不要在转换过程中修改状态 —— 在exit()内部调用transition_to()?重入式转换会导致未定义行为。应标记转换请求,在当前操作完成后再执行。
- 绝对不要跳过状态验证 —— 调用但该状态不存在?会导致静默失败或崩溃。转换前必须验证状态是否存在。
transition_to("AttackState") - 绝对不要忘记处理子状态 —— 带有子状态的分层状态?父状态的必须调用
update()以实现委托处理。current_child_state.update() - 绝对不要在代码中使用字符串类型的状态名称 —— 输入错误如会导致静默失败。请使用常量或枚举:
transition_to("Idel")。transition_to(States.IDLE)
gdscript
undefinedhierarchical_state.gd
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)undefinedclass_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)undefinedState Base Class
状态基类
gdscript
undefinedgdscript
undefinedstate.gd
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
undefinedclass_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
undefinedBest Practices
最佳实践
- Separation - One state per file
- Signals - Communicate state changes
- Stack - Use push/pop for interruptions
- 分离原则 - 每个状态对应一个文件
- 信号机制 - 通过信号传递状态变更消息
- 栈的使用 - 利用push/pop处理中断场景
Reference
参考
- Related: ,
godot-characterbody-2dgodot-animation-player
- 相关资源:,
godot-characterbody-2dgodot-animation-player
Related
相关技能
- Master Skill: godot-master
- 核心技能:godot-master