UE5.8 Smart Objects 专题系列

UE5.8 Smart Objects 专题(八):StateTree 与 GameplayInteractions 接入

讲清 GameplayInteractions 如何把 Smart Object 行为交给 StateTree,包含 StateTreeReference、Schema、Find Slot、Send/Listen Slot Event、Set Slot Enabled 和失败回退。

总览

GameplayBehavior 适合“一段行为配置”,但复杂交互往往是流程:进入、对齐、播放动作、等待事件、改变 Slot Tag、处理失败、退出恢复。UE5.8 的 GameplayInteractions 插件把 Smart Object 行为定义接到 StateTree,让交互可以用状态树编排。

UE5.8 Smart Objects 专题(八):StateTree 与 GameplayInteractions 接入 配图
GameplayInteractions 让一个 Smart Object 的使用过程可以由 StateTree 编排,而不只是播放一个行为配置。

使用案例

工作台流程:

  1. Claim Activity.Work.Craft Slot。
  2. MoveTo 入口。
  3. StateTree 进入 Approach 状态,检查距离和朝向。
  4. 进入 Use 状态,发送 Slot Event Event.Work.Start
  5. 给 Slot 加 RuntimeTag SO.State.InUse,播放加工动画。
  6. 收到 Event.Work.Done 或计时结束。
  7. Exit 时移除 Tag,释放 Claim。

这个流程如果写在蓝图 Tick 里会很乱;StateTree 能把每个阶段的 Enter、Tick、Exit、Transition 和失败回退拆开。

架构分析

UGameplayInteractionSmartObjectBehaviorDefinition 继承 USmartObjectBehaviorDefinition,核心属性是 FStateTreeReference StateTreeReference。它指定“使用这个 Slot 时运行哪棵 StateTree”。UGameplayInteractionStateTreeSchema 允许配置 Context Actor Class 和 SmartObject Actor Class,并提供 StateTree 运行所需的上下文描述。

GameplayInteractions 还提供一组 Smart Object 相关 StateTree Task。比如 Find Slot 可以基于 ActivityTag 或 Slot Link Annotation 找关联 Slot;Send Slot Event 可以在 Enter/Exit 发送事件;Listen Slot Events 把 Slot 事件转成 StateTree 事件;Set Slot Enabled 可以在某个状态期间临时禁用 Slot。

StateTree 设计建议

Root
  Approach
    Task: Move To Entrance
    Transition: Arrived -> Align
  Align
    Task: Face Slot
    Transition: Done -> Use
  Use
    Task: Send Slot Event Event.Work.Start
    Task: Play Montage / Wait Event
    Transition: Done -> Exit
  Exit
    Task: Send Slot Event Event.Work.End

关键是 ExitState 必须清理:停止动画、撤销临时 Tag、恢复 Slot Enabled、释放 Claim。StateTree 的价值不只是“状态可视化”,更是把清理写在确定生命周期里。

项目落地

把 GameplayInteractions 用在“对象驱动的多阶段交互”上:坐椅子、工作台、门、机关、攀爬、双人同步动作。不要把所有 AI 决策都塞进这棵 StateTree;外层 AI 仍然决定“我要不要去用工作台”,Smart Object 的 StateTree 决定“已经开始用以后每一步怎么执行”。

常见坑

  • StateTree 里重新 Find 同一个对象:Claim 之后应该围绕 ClaimHandle/SlotHandle 做流程。
  • Slot Event 没有命名规范:事件 Tag 要分域,例如 Event.SO.Work.Start
  • Exit 清理漏掉:StateTree 外部 Stop、失败、重选都要处理。
  • 把 GameplayInteractions 当 Mass 专用:它可以用于普通 Actor 交互,不只服务 Mass。
  • 私有 Task 直接硬依赖:项目代码应通过插件暴露的行为定义和编辑器节点使用,避免复制引擎私有头。

源码依据

UGameplayInteractionSmartObjectBehaviorDefinition 保存 FStateTreeReference,并使用 GameplayInteractionStateTreeSchemaUGameplayInteractionStateTreeSchema 允许绑定 Context Actor 和 SmartObject Actor。GameplayInteractions 的 StateTree Task 包括 FGameplayInteractionFindSlotTaskFGameplayInteractionSendSlotEventTaskFGameplayInteractionListenSlotEventsTaskFGameplayInteractionSetSlotEnabledTaskFGameplayInteractionModifySlotTagTaskFGameplayInteractionSyncSlotTagStateTask

源码路径索引

  • GameplayInteractionsModule/Public/GameplayInteractionSmartObjectBehaviorDefinition.h
  • GameplayInteractionsModule/Public/GameplayInteractionStateTreeSchema.h
  • GameplayInteractionsModule/Private/StateTree/GameplayInteractionFindSlotTask.h
  • GameplayInteractionsModule/Private/StateTree/GameplayInteractionSendSlotEventTask.h
  • GameplayInteractionsModule/Private/StateTree/GameplayInteractionSetSlotEnabledTask.h
  • GameplayInteractionsModule/Private/StateTree/GameplayInteractionListenSlotEventsTask.h