68 lines
4.4 KiB
Markdown
68 lines
4.4 KiB
Markdown
# BP_Hud
|
||
|
||
## 基本信息
|
||
- **类型**: Blueprint (HUD)
|
||
- **父类**: HUD
|
||
- **源文件**: /Game/Blueprints/BP_Hud.BP_Hud
|
||
- **模块**: Content/Blueprints
|
||
|
||
## 功能概述
|
||
游戏主 HUD,管理三个 UI 窗口的引用:物品栏视图(InvView → WBP_InventoryView)、库存窗口(InventoryWindow → WBP_Window)、提示窗口(HintWindow → WBP_Window),并在 EventGraph 中引用 BP_UniversalEndpointComp 处理端点通信。作为 UI 系统的根容器,负责 Widget 的创建和生命周期管理。
|
||
|
||
## 设计用意
|
||
选择继承 Engine.HUD 而非在 PlayerController 或 Pawn 中管理 UI,是因为 HUD 是 UE 原生提供的 UI 渲染层入口,拥有独立的绘制生命周期(DrawHUD),且不干扰游戏逻辑层的更新频率。将三个核心窗口的引用集中到 HUD 后,其他系统只需要通过 PlayerController→GetHUD() 即可访问所有 UI 组件,避免了全局单例或漫长的引用传递链。
|
||
|
||
## 职责范围
|
||
- 在游戏初始化时创建 InvView、InventoryWindow、HintWindow 三个 Widget
|
||
- 持有这三个 Widget 的引用,作为其他系统获取 UI 实例的入口
|
||
- 不介入各 Widget 内部的具体交互逻辑(物品渲染、窗口拖拽、提示内容由各自 Widget 负责)
|
||
- 不处理输入事件分发。
|
||
|
||
## 项目内依赖
|
||
| 依赖项 | 关系 | 源文件 |
|
||
|--------|------|--------|
|
||
| WBP_InventoryView | 包含(变量InvView) | /Game/Blueprints/UI/Item/WBP_InventoryView |
|
||
| WBP_Window | 包含(变量InventoryWindow, HintWindow) | /Game/Blueprints/UI/WBP_Window |
|
||
| WBP_Hint | 引用(EventGraph) | /Game/Blueprints/UI/WBP_Hint |
|
||
| BP_UniversalEndpointComp | 引用(EventGraph) | /Game/Blueprints/Component/BP_UniversalEndpointComp |
|
||
|
||
## 外部视角
|
||
|
||
BP_Hud 是纯蓝图 HUD(无 C++ 子类),从外部调用者视角:
|
||
|
||
**公开 Widget 变量(蓝图可读写)**
|
||
- `InvView`(WBP_InventoryView_C*)— 物品栏视图 Widget 引用
|
||
- `InventoryWindow`(WBP_Window_C*)— 库存窗口 Widget 引用
|
||
- `HintWindow`(WBP_Window_C*)— 提示窗口 Widget 引用
|
||
|
||
上述三个变量均标记为 InstanceEditable,其他蓝图可通过 `GetPlayerController→GetHUD→Cast To BP_Hud_C` 访问。HUD 本身无自定义 BlueprintCallable 函数,所有交互通过操作这三个 Widget 引用间接完成。
|
||
|
||
**EventGraph 事件**
|
||
|
||
| 事件 | 类型 | 触发时机 | 行为 |
|
||
|------|------|----------|------|
|
||
| `事件开始运行` | 原生覆盖 (BeginPlay) | HUD 创建时由引擎自动触发 | 创建 InventoryWindow、InvView、HintWindow 三个 Widget,添加到视口;绑定 BP_UniversalEndpointComp 的 Command Received 委托到 DealWithCommand |
|
||
| `DealWithCommand` | 自定义事件 | 收到 BP_UniversalEndpointComp 的指令数据包时 | 解析 CommandPacket → 提取 DiscreteMeta 中的 GameplayTag → 若匹配 "Gameplay.Action.UI.Hint" 则切换 HintWindow 显示/隐藏,否则切换 InventoryWindow 显示/隐藏 |
|
||
|
||
**外部交互方式**
|
||
- 游戏模式在启动时自动创建 BP_Hud,并初始化三个子 Widget
|
||
- 外部系统获取 BP_Hud 实例后,通过读取其 Widget 变量访问 UI 组件
|
||
- 指令路由系统通过 BP_UniversalEndpointComp 间接向 BP_Hud 发送 UI 控制指令
|
||
|
||
## 使用方法
|
||
|
||
BP_Hud 是纯蓝图 HUD,在项目中通过以下调用链工作:
|
||
|
||
- **游戏初始化流程** — 引擎启动游戏 → BP_TestMode 将 BP_Hud 设为 HUDClass → 生成 BP_Hud 实例 → 触发`事件开始运行` → 创建 InvView/InventoryWindow/HintWindow 三个 Widget 并添加到视口
|
||
- **指令驱动的 UI 切换流程** — 外部系统(如 BP_ControllerComp)发送 CommandPacket → BP_UniversalEndpointComp 接收并派发 → BP_Hud 的 `DealWithCommand` 处理 → 解析 GameplayTag → 切换 HintWindow 或 InventoryWindow 的可见性
|
||
- **外部访问 UI 组件流程** — 任何蓝图(如 BP_Barrel、BP_InventoryComp)→ GetPlayerController → GetHUD → Cast To BP_Hud_C → 直接读写 InvView/InventoryWindow/HintWindow 变量
|
||
|
||
## 用例
|
||
|
||
| 引用方 | 路径 | 用途 |
|
||
|--------|------|------|
|
||
| BP_TestMode | `/Game/Blueprints/BP_TestMode` | 将 BP_Hud 设为 HUDClass,作为游戏全局 UI 根容器 |
|
||
| BP_TestCtl | `/Game/Blueprints/BP_TestCtl` | 通过 GetHUD 获取 BP_Hud,访问子 Widget |
|
||
| BP_Barrel | `/Game/Blueprints/Playground/BP_Barrel` | 通过 BP_Hud 获取 InventoryWindow 和 InvView,实现木桶 UI |
|
||
| BP_InventoryComp | `/Game/Blueprints/Playground/BP_InventoryComp` | 引用 BP_Hud 用于库存 UI 联动 |
|