跳转至

条件与行为资源

状态脚本可重写 _can_enter()_can_exit() 作为进入/退出守卫。状态组会在守卫拒绝时发出 transition_blocked,并保持当前状态不变。

需要把可复用条件和行为放到资源里时,可以继承 GFNodeStateConditionGFNodeStateBehavior,再挂到状态的 enter_conditionsexit_conditionsbehaviors 数组上。

条件会和脚本守卫一起决定是否允许切换。行为会在状态自己的 _initialize()_enter()_exit()_pause()_resume()_handle_state_event() 之后运行,适合复用动画播放、音效、输入门禁、调试标记等横切逻辑。

class_name HasTargetCondition
extends GFNodeStateCondition


func _evaluate(state: GFNodeState, _phase: StringName, _peer_state: StringName = &"", _args: Dictionary = {}) -> bool:
    return state.get_blackboard().has("target")
class_name PlayStateAudioBehavior
extends GFNodeStateBehavior


func _enter(state: GFNodeState, _previous_state: StringName = &"", _args: Dictionary = {}) -> void:
    var host := state.get_host()
    if host != null and host.has_method("play_state_audio"):
        host.call("play_state_audio", state.get_state_name())

复杂状态仍应继续写成 GFNodeState 子类。Resource 钩子适合抽出可组合、可在 Inspector 复用的通用片段。

需要在同一状态组内共享少量运行时上下文时,可使用 GFNodeStateGroup.blackboard 或状态内的 get_blackboard();字段含义仍由项目层决定。