4.1 KiB
4.1 KiB
UEndpointComponent
基本信息
- 类型: UCLASS(BlueprintSpawnableComponent, Blueprintable)
- 父类: UActorComponent, implements ICommandEndpoint
- 源文件: Plugins/CharacterControl/Source/CharacterControl/Public/EndpointComponent.h
- 模块: CharacterControl
功能概述
用于蓝图编写的端点的基类 ActorComponent。持有自身的 FEndpointState 和委托。ReceiveCommand 调用 OnCommandReceived(BlueprintImplementableEvent)。BeginPlay 自动向同级 UCommandRouterComponent 注册。
设计用意
蓝图端点的主要基类。在蓝图中子类化,重写 OnCommandReceived。默认设置下与 UCommandRouterComponent 自动连线。Blueprintable 元数据明确鼓励蓝图子类化。
职责范围
蓝图友好的端点基类。处理状态、委托和自动注册。OnCommandReceived 事件是蓝图扩展点。
项目内依赖
| 依赖项 | 关系 | 源文件 |
|---|---|---|
| CommandEndpoint.h | #include | Plugins/CharacterControl/Source/CharacterControl/Public/CommandEndpoint.h |
对外接口
UEndpointComponent 是蓝图端点的主要基类。BlueprintSpawnableComponent + Blueprintable 允许直接在蓝图中创建子类并重写事件。
蓝图可重写事件:
- OnCommandReceived(const FCommandPacket&) (BlueprintImplementableEvent): 蓝图的主要扩展点。当路由器投递命令到此端点时调用。蓝图子类重写此事件实现具体逻辑(移动、技能、UI 等)。C++ 侧 ReceiveCommand 直接调用此事件
委托(C++ 访问):
- OnEndpointStateChanged (FOnEndpointStateChanged, BlueprintReadOnly): 状态变更委托。被路由器订阅以触发标签聚合更新
- OnCommandOutput (FOnCommandOutput, BlueprintReadOnly): 命令输出委托。端点产出命令时通过此委托广播,由路由器接收并转发
配置:
- EndpointState (FEndpointState, EditAnywhere): 端点的身份和配置。在组件详情面板中设置 InterestedTags、bIsActive、bIsContinuousFriendly、bIsAsynchronous。EndpointGuid 自动生成
自动注册:BeginPlay 时 UEndpointComponent 主动查找兄弟 UCommandRouterComponent 并通过其 InternalRouter->RegisterEndpoint(this) 注册。只需将端点组件添加到有 UCommandRouterComponent 的 Actor 上即可自动接入本地命令总线。
使用方法
蓝图子类化 UEndpointComponent 并配置 EndpointState 是最常见的端点创建方式:
- 构造函数初始化:
Plugins/CharacterControl/Source/CharacterControl/Private/EndpointComponent.cpp:8-14-- 构造函数中设置 bIsActive=true,禁用 Tick - ReceiveCommand 桥接:
Plugins/CharacterControl/Source/CharacterControl/Private/EndpointComponent.cpp:16-19-- ReceiveCommand 直接调用 OnCommandReceived(Command),将 C++ 接口调用桥接到蓝图事件 - BeginPlay 自动注册:
Plugins/CharacterControl/Source/CharacterControl/Private/EndpointComponent.cpp:22-42-- 查找 Owner 上的 UCommandRouterComponent,通过OnCommandOutput.IsBound()判断是否已注册,未注册则调用Router->InternalRouter->RegisterEndpoint(this) - 注册到第一个路由器:
Plugins/CharacterControl/Source/CharacterControl/Private/EndpointComponent.cpp:39--break语句确保只注册到第一个找到的 UCommandRouterComponent
用例
- 蓝图端点实现: 创建 Blueprint 类继承 UEndpointComponent,在 EndpointState 中配置 InterestedTags(如 "Input.Move", "Action.Jump"),在 Event Graph 中重写 OnCommandReceived 实现具体逻辑
- 自动连线:
Plugins/CharacterControl/Source/CharacterControl/Private/EndpointComponent.cpp:22-42-- BeginPlay 自动找到兄弟 UCommandRouterComponent 并注册。策划只需将组件拖入 Actor 即可,无需手动连线 - 重复注册防护:
Plugins/CharacterControl/Source/CharacterControl/Private/EndpointComponent.cpp:34-37-- 通过检查 OnCommandOutput.IsBound() 判断是否已注册,避免重复注册导致标签聚合错误 - 只注册到一个路由器:
Plugins/CharacterControl/Source/CharacterControl/Private/EndpointComponent.cpp:39-- break 退出循环,一个端点只注册到一个路由器