update
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
- **类型**: Blueprint (ActorComponent)
|
||||
- **父类**: ActorComponent
|
||||
- **源文件**: /Game/Blueprints/Playground/BP_InventoryComp.BP_InventoryComp
|
||||
- **模块**: Content
|
||||
- **模块**: Content/Blueprints/Playground
|
||||
|
||||
## 功能概述
|
||||
BP_InventoryComp 是库存管理组件,包装了一个 UDefaultContainer 实例,持有物品视图缓存和视图变化委托,提供物品创建和视图更新功能。它是物品系统与 Actor 之间的桥梁组件,也是 IInventory 接口的蓝图实现者。
|
||||
@@ -23,32 +23,55 @@ BP_InventoryComp 负责持有并管理 UDefaultContainer 实例、维护 FItemVi
|
||||
| FItemView | 缓存 (ViewCache) | Item 插件 |
|
||||
| IInventory | 实现 | Item 插件 |
|
||||
|
||||
## 对外接口
|
||||
- **蓝图函数**:
|
||||
- `UpdateViewCache()` -- 刷新 ViewCache,从 DefaultContainer 重新获取物品视图列表,同步缓存与底层容器状态
|
||||
- `CreateItemInternal(FName ItemType, int32 Count) -> bool` -- 内部物品创建函数,通过 DefaultContainer.CreateItem 创建物品后更新 ViewCache
|
||||
- **蓝图变量**(均为 is_instance_editable):
|
||||
- `DefaultContainer` (DefaultContainer*/UDefaultContainer*) -- 底层物品容器引用,默认指向 BP_DefaultContainer
|
||||
- `ViewCache` (TArray<FItemView>) -- 物品视图缓存数组,UI 从此读取显示数据,结构与 FItemView 一致(ItemID, ItemType, ItemName, ItemDescription, Icon, ItemDataText)
|
||||
- `OnViewChanged` (动态多播委托) -- 视图变化通知委托,每次 ViewCache 更新时广播,供 UI 绑定自动刷新
|
||||
- **IInventory 接口实现**(BlueprintNativeEvent):
|
||||
- `GetItemViews() -> TArray<FItemView>` -- 返回所有物品视图
|
||||
- `GetItemViewByID(const FGuid& ItemID) -> FItemView` -- 按 ID 获取视图
|
||||
- `GetItemCount() -> int32` -- 物品数量
|
||||
- `RequestMoveItem(const FGuid& ItemID, const TScriptInterface<IInventory>& TargetInventory)` -- 请求移动物品到目标库存
|
||||
- `ReceiveItem(const FGuid& ItemID, const TScriptInterface<IItemContainer>& SourceContainer)` (BlueprintAuthorityOnly) -- 从源容器接收物品
|
||||
- **EventGraph**:含 7 个事件和 30 个节点,处理库存初始化和生命周期
|
||||
## 外部视角
|
||||
|
||||
BP_InventoryComp 是 ActorComponent 的子类(无 C++ 父类),从外部调用者视角:
|
||||
|
||||
**公开属性(蓝图可读写)**
|
||||
- `DefaultContainer`(UDefaultContainer*)— 关联的物品容器实例
|
||||
- `ViewCache`(TArray<FItemView>)— 物品视图缓存
|
||||
|
||||
**公开委托**
|
||||
- `OnViewChanged`(多播委托)— 视图变化时广播,外部可绑定。触发时机:物品创建、移动、移除等导致 ViewCache 变更时
|
||||
|
||||
**自定义函数**
|
||||
- `UpdateViewCache()` — 从 DefaultContainer 刷新 ViewCache 并触发 OnViewChanged
|
||||
- `CreateItemInternal(FName ItemType, int32 Count)` → `bool` — 创建指定类型和数量的物品并放入容器
|
||||
|
||||
**外部交互方式**
|
||||
- 作为组件添加到 Actor(如 BP_Barrel、BP_NewChar)
|
||||
- UI 层(WBP_InventoryView)通过 Comp 变量引用,读取 ViewCache 渲染列表,绑定 OnViewChanged 监听变化
|
||||
- 其他系统通过组件引用的方式获取 BP_InventoryComp 并进行物品操作
|
||||
|
||||
**EventGraph 事件**
|
||||
|
||||
| 事件 | 类型 | 触发时机 | 行为 |
|
||||
|------|------|----------|------|
|
||||
| `事件开始运行` | 原生覆盖 (BeginPlay) | 组件创建时由引擎自动触发 | 创建 BP_DefaultContainer → 判断网络权限:服务器端执行 GetOwner → Cast 到 Pawn → Controller → PlayerController → HUD → BP_Hud → InvView → BindToComp |
|
||||
| `CreateItemRPCReplicated` | 自定义事件 (RPC, Server) | 客户端请求创建物品时 | 接收 ItemType 和 Count → 通过 IItemContainer 接口创建物品 |
|
||||
| `MoveItemRPCReplicated` | 自定义事件 (RPC, Server) | 客户端请求移动物品时 | 接收 TargetInventory 和 ItemID → 通过 IInventory 接口移动物品到目标容器 |
|
||||
| `UpdateViewCacheRPCReplicated` | 自定义事件 (RPC, Server) | 客户端请求刷新视图时 | 从 DefaultContainer 获取 ItemViews → 更新 ViewCache |
|
||||
| `SyncViewCacheRPCReplicated` | 自定义事件 (RPC, All) | 服务器同步视图到所有客户端时 | 接收服务器端 ViewCache → 更新本地 ViewCache → 调用 OnViewChanged 委托通知 UI |
|
||||
| `Client RPC` | 自定义事件 (RPC, Client) | 服务器通知客户端刷新 UI 时 | 获取 Owner → Cast 到 Pawn → Controller → PlayerController → HUD → BP_Hud → InvView → BindToComp |
|
||||
|
||||
## 使用方法
|
||||
- 作为 ActorComponent 添加到需要库存功能的 Actor 上
|
||||
- 设置 `DefaultContainer` 变量指向 BP_DefaultContainer 资产(或其他 IItemContainer 实现)
|
||||
- UI (如 WBP_InventoryView) 绑定 `OnViewChanged` 委托,在库存变化时自动刷新
|
||||
- 调用 `CreateItemInternal(ItemType, Count)` 创建新物品,之后 ViewCache 自动更新
|
||||
- 通过 IInventory 接口与其他库存组件交互:`RequestMoveItem` 将物品移出,`ReceiveItem` 从其他容器接收物品
|
||||
- `UpdateViewCache` 在物品变化后同步缓存与底层容器的状态
|
||||
- 继承类 BP_DropItemInvComp 通过覆盖 BeginPlay 自动调用 CreateItemInternal
|
||||
|
||||
BP_InventoryComp 在项目中的典型调用流程:
|
||||
|
||||
- **库存初始化流程** — Actor(如 BP_Barrel)的 BP_InventoryComp 组件创建 → BeginPlay → 创建 BP_DefaultContainer → 服务端连接 HUD 的 InvView 绑定 UI
|
||||
- **物品创建流程** — 客户端请求创建物品 → CreateItemRPCReplicated(RPC 到服务器)→ 通过 IItemContainer.CreateItem 创建 → 服务器触发 SyncViewCacheRPC → 更新所有客户端 ViewCache → OnViewChanged 通知 UI
|
||||
- **UI 绑定流程** — WBP_InventoryView.BindToComp → 设置 Comp 变量引用 → 绑定 OnViewChanged 委托 → 库存变化时自动调用 UpdateListItems
|
||||
|
||||
## 用例
|
||||
- BP_DropItemInvComp 的父类,提供库存管理的通用基础
|
||||
- WBP_InventoryView 通过 `Comp` 变量引用此组件,读取 ViewCache 渲染物品列表
|
||||
- 添加到任何 Actor 上使其具备库存能力
|
||||
|
||||
BP_InventoryComp 被 11 个文件引用(项目内最广泛的组件之一),按用途分类:
|
||||
|
||||
| 引用方 | 路径 | 用途 |
|
||||
|--------|------|------|
|
||||
| WBP_InventoryView | `/Game/Blueprints/UI/Item/WBP_InventoryView` | 读取 ViewCache 渲染 UI,绑定 OnViewChanged 自动刷新 |
|
||||
| BP_DropItemInvComp | `/Game/Blueprints/Playground/BP_DropItemInvComp` | 继承为掉落物组件,增加 Item 变量 |
|
||||
| BP_Barrel | `/Game/Blueprints/Playground/BP_Barrel` | 组件实例,管理木桶内物品 |
|
||||
| BP_InventoryDrop | `/Game/Blueprints/Playground/BP_InventoryDrop` | 组件实例,管理掉落物品 |
|
||||
| BP_ItemTrap | `/Game/Blueprints/Playground/BP_ItemTrap` | 组件实例,管理陷阱物品 |
|
||||
| BP_TestItem | `/Game/Blueprints/Playground/BP_TestItem` | 组件实例,测试物品管理 |
|
||||
| BP_Bomb | `/Game/Blueprints/Playground/BP_Bomb` | 组件实例,管理炸弹物品 |
|
||||
|
||||
Reference in New Issue
Block a user