# UEndpointDispatcher ## 基本信息 - **类型**: UCLASS(BlueprintType) - **父类**: UObject - **源文件**: Plugins/CharacterControl/Source/CharacterControl/Public/CommandEndpoint.h - **模块**: CharacterControl ## 功能概述 向蓝图公开端点委托的桥接对象。持有 OnStateChanged 和 OnCommandOutput 委托,以及 CurrentState。为蓝图调用者提供 NotifyStateChanged/OutputCommand 包装 UFUNCTION。 ## 设计用意 C++ 委托与蓝图虚拟机之间的粘合剂。蓝图端点实现通过 GetEndpointDispatcher_BP 返回此对象。ICommandEndpoint 的默认 C++ 实现均通过此调度器进行路由。 ## 职责范围 D蓝图端点的委托桥接。向 C++ 观察者广播状态变更和命令输出。不实现端点逻辑。 ## 项目内依赖 | 依赖项 | 关系 | 源文件 | |--------|------|--------| | (none) | | | ## 对外接口 UEndpointDispatcher 是蓝图端点与 C++ 委托系统之间的桥接对象。它暴露委托引用供 C++ 直接访问,同时提供 UFUNCTION 包装供蓝图调用。 - **OnStateChanged** (FOnEndpointStateChanged, public field): C++ 直接访问的委托。当端点状态变更时广播。被 UCommandRouter 通过 BindDynamic 订阅以触发聚合更新 - **OnCommandOutput** (FOnCommandOutput, public field): C++ 直接访问的委托。当端点输出命令时广播。被 UCommandRouter 订阅以转发端点产出的命令 - **CurrentState** (FEndpointState, EditAnywhere, BlueprintReadWrite): 端点当前状态的副本,ICommandEndpoint::GetEndpointState 默认实现返回此字段 - **NotifyStateChanged** (UFUNCTION, BlueprintCallable): 蓝图广播入口,调用后执行 OnStateChanged.ExecuteIfBound(NewState),C++ 订阅者收到通知 - **OutputCommand** (UFUNCTION, BlueprintCallable): 蓝图广播入口,调用后执行 OnCommandOutput.ExecuteIfBound(Command),将命令推送到路由系统 蓝图端点实现 ICommandEndpoint 时,需重写 GetEndpointDispatcher_BP 返回调度器实例。ICommandEndpoint 的 C++ 默认实现全部通过此调度器工作。 ## 使用方法 UEndpointDispatcher 是 ICommandEndpoint 蓝图实现的内部机制: - **C++ 默认桥接**: `Plugins/CharacterControl/Source/CharacterControl/Private/CommandEndpoint.cpp:8-26` -- ICommandEndpoint 的四个纯虚函数默认实现全部通过 GetEndpointDispatcher_BP 获取调度器后委托执行 - **GetEndpointState 默认实现**: `Plugins/CharacterControl/Source/CharacterControl/Private/CommandEndpoint.cpp:10` -- 返回 `Execute_GetEndpointDispatcher_BP(...)->CurrentState` - **ReceiveCommand 默认实现**: `Plugins/CharacterControl/Source/CharacterControl/Private/CommandEndpoint.cpp:25` -- 调用 `Execute_ReceiveCommand_BP(..., Command)` - **GetStateChangedDelegate 默认实现**: `Plugins/CharacterControl/Source/CharacterControl/Private/CommandEndpoint.cpp:15` -- 返回调度器的 OnStateChanged 引用 - **NotifyStateChanged 内联**: `Plugins/CharacterControl/Source/CharacterControl/Public/CommandEndpoint.h:140-143` -- 函数体内直接 ExecuteIfBound - **OutputCommand 内联**: `Plugins/CharacterControl/Source/CharacterControl/Public/CommandEndpoint.h:146-149` -- 函数体内直接 ExecuteIfBound ## 用例 - **蓝图端点状态通知**: 蓝图端点实现类调用 NotifyStateChanged,调度器转发给所有绑定的 C++ 订阅者(如 UCommandRouter::OnRegisteredEndpointStateChanged),触发标签聚合更新 - **蓝图端点命令输出**: 蓝图端点调用 OutputCommand 将命令包推入路由系统,调度器广播 OnCommandOutput,由 UCommandRouter::OnRegisteredEndpointOutputCommand 接收处理 - **C++ 端点绕过调度器**: `Plugins/CharacterControl/Source/CharacterControl/Public/CommandRouter.h:22-25` -- UCommandRouter 作为 C++ 端点直接重写 GetEndpointState/ReceiveCommand 等方法,不使用调度器;`Plugins/CharacterControl/Source/CharacterControl/Public/EndpointComponent.h:30-33` -- UEndpointComponent 同样直接重写