Files
loneseDocument/Plugins/Item/UItemRegistrySettings.md
meishibiezb 29a3f77908 init
2026-06-04 21:44:13 +08:00

84 lines
4.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# UItemRegistrySettings
## 基本信息
- **类型**: UCLASS(Config=Game, DefaultConfig)
- **父类**: UDeveloperSettings
- **源文件**: Plugins/Item/Source/Item/Public/ItemRegistrySettings.h
- **模块**: Item
## 功能概述
Item插件的项目设置类。可在编辑器中的 Project Settings > Game > Item Registry Settings 下配置。包含DataTable软引用和视图策略类软引用的TMap。软引用避免启动时急切加载。
## 设计用意
设计时配置的契约。UDeveloperSettings将配置存储在DefaultGame.ini中。软引用避免在启动时加载所有内容。标准的UE项目设置模式。
## 职责范围
配置数据容器。将名称映射到DataTable/策略类资产。由UItemRegistrySubsystem::Initialize在游戏启动时读取。无运行时逻辑。
## 项目内依赖
(无项目内依赖)
## 对外接口
UItemRegistrySettings 是 UDeveloperSettings 子类Config=Game, DefaultConfig作为项目配置容器本身没有可调用的方法仅有默认构造函数。外部通过以下两个 UPROPERTY 读取配置:
- **ItemDataTables** (TMap<FName, TSoftObjectPtr<UDataTable>>): 物品 DataTable 的映射表。键为逻辑名称(如 "Default"),值为 DataTable 资产的软引用路径。在编辑器中通过 Project Settings > Game > Item Registry Settings 配置。
- **ViewStrategies** (TMap<FName, TSoftClassPtr<UObject>>): 视图策略类的映射表。键为属性名称(对应 IItemViewStrategy::GetPropertyName值为实现了 ItemViewStrategy 接口的 Blueprint 类软引用。
**读取方式:**
由 UItemRegistrySubsystem::Initialize 通过 GetDefault<UItemRegistrySettings>() 获取单例,遍历两个 TMap 调用 LoadSynchronous 加载软引用并注册ItemRegistrySubsystem.cpp:58-80
**配置存储:**
标记 Config=Game 使这些字段存储在 DefaultGame.ini 中。设计师不需要修改代码,只需在项目设置编辑器中配置 DataTable 和策略类的映射关系即可。软引用TSoftObjectPtr / TSoftClassPtr避免在游戏启动时急切加载所有资产。
## 使用方法
UItemRegistrySettings 是纯配置数据容器,由研发人员在项目设置中配置。
**配置路径:**
编辑器中Project Settings > Game > Item Registry Settings由 meta = (DisplayName = "Item Registry Settings") 定义)。
**配置 ItemDataTables**
在 "General" 分类下,添加键值对:
-DataTable 的逻辑名称(如 "Items"、"Equipment")。
- 值:指向 DataTable 资产(其行结构体为 FItemDef的软引用。
**配置 ViewStrategies**
在 "Strategies" 分类下,添加键值对:
- 键:属性名称(与 IItemViewStrategy::GetPropertyName() 返回值一致,如 "Attribute_Health")。
- 值:指向实现了 ItemViewStrategy 接口的 Blueprint 类的软引用。
**运行时加载ItemRegistrySubsystem.cpp:58-80**
```cpp
void UItemRegistrySubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
const UItemRegistrySettings* Settings =
GetDefault<UItemRegistrySettings>();
for (const auto& Pair : Settings->ItemDataTables)
{
if (UDataTable* LoadedTable = Pair.Value.LoadSynchronous())
RegisterDataTable(Pair.Key, LoadedTable);
}
for (const auto& Pair : Settings->ViewStrategies)
{
if (UClass* StrategyClass = Pair.Value.LoadSynchronous())
{
TScriptInterface<IItemViewStrategy> NewStrategy =
NewObject<UObject>(this, StrategyClass);
RegisterViewStrategy(Pair.Key, NewStrategy);
}
}
}
```
**配置存储位置:**
标记 Config=Game 使配置写入 DefaultGame.ini。多个平台可共享同一配置也可按平台覆写。
## 用例
- `Plugins/Item/Source/Item/Public/ItemRegistrySettings.h:13-23` -- UItemRegistrySettings 类声明ItemDataTables + ViewStrategies 两个 UPROPERTY + 构造函数)。
- `Plugins/Item/Source/Item/Private/ItemRegistrySettings.cpp:6-8` -- 构造函数实现(空实现,所有工作在 UPROPERTY 初始化器中完成)。
- `Plugins/Item/Source/Item/Private/ItemRegistrySubsystem.cpp:59` -- Initialize 中通过 GetDefault<UItemRegistrySettings>() 获取配置单例。
- `Plugins/Item/Source/Item/Private/ItemRegistrySubsystem.cpp:62-68` -- Initialize 中遍历 Settings->ItemDataTablesLoadSynchronous 每个软引用并注册。
- `Plugins/Item/Source/Item/Private/ItemRegistrySubsystem.cpp:70-78` -- Initialize 中遍历 Settings->ViewStrategiesLoadSynchronous 每个类NewObject 实例化策略并注册。
- `Plugins/Item/Source/Item/Private/ItemRegistrySubsystem.cpp:79-80` -- Initialize 完成后通过 UE_LOG 输出注册的 DataTable 和策略数量。