道具定义

avatar`
Yuewu(罗传月武)
Updated: Jun 28, 2025

介绍

本文介绍如何在GIS中创建和管理道具定义。

创建和管理道具定义

道具定义.案例

在编辑器中创建类型为GIS_ItemDefinition的数据资产,即可创建一个新的道具。在定义中你可以:

  1. DisplayName/Description/Icon:你可以指定该道具用于UI的显示信息。
  2. ItemTags:通过ItemTags为道具关联一组GameplayTags,可用于道具查询,分类,数据验证等。
  3. Unique:表示道具的唯一性,唯一的道具不可堆叠。
  4. Fragments:可以添加不同类型的数据片段,以构成复杂的道具数据结构。


道具数据片段

道具定义.片段

你可以通过添加Fragements来扩充道具的数据,GIS内置了一些常见的道具片段:

  1. Fragment_Attributes:允许你为道具添加初始属性,在道具实例创建时,会将指定的属性添加到道具实例中。
  2. Fragment_EquippableItem:允许你将该道具关联一个装备定义,在道具被添加到“用于装备的集合”时,装备组件会根据这个信息创建对应的装备实例。
  3. Fragment_Shoppable:允许你定义该道具通过商店买卖时,所需的货币类型及其数量。(10个石头,3个木头。)

添加新的道具片段

用户可以通过蓝图/C++创建GIS_ItemFragement的子类以新增片段类型,新增的片段类型会自动出现在Fragments数组的选项中。

每一个Fragment可以重写OnInstancedCreated函数以做一些初始化工作,这个函数在道具实例被创建时调用。

通过C++

下面是Fragment_Attributes的实现案例。

1/**
2 * An item fragment which adding attributes to item instance.
3 */
4UCLASS(DisplayName="ItemFragment_Attributes")
5class GENERICINVENTORYSYSTEM_API UGIS_ItemFragment_SetAttributes : public UGIS_ItemFragment
6{
7 GENERATED_BODY()
8
9protected:
10 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category=Attribute, meta=(TitleProperty="{Tag} -> {Value}"))
11 TArray<FGIS_GameplayTagFloat> InitialFloatAttributes;
12 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category=Attribute, meta=(TitleProperty="{Tag} -> {Value}"))
13 TArray<FGIS_GameplayTagBool> InitialBoolAttributes;
14 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category=Attribute, meta=(TitleProperty="{Tag} -> {Value}"))
15 TArray<FGIS_GameplayTagInteger> InitialIntegerAttributes;
16
17public:
18 virtual void OnInstanceCreated(UGIS_ItemInstance* Instance) const override;
19
20 float GetFloatAttributeByTag(FGameplayTag Tag) const;
21 bool GetBoolAttributeByTag(FGameplayTag Tag) const;
22 int32 GetIntegerAttributeByTag(FGameplayTag Tag) const;
23};
1void UGIS_ItemFragment_SetAttributes::OnInstanceCreated(UGIS_ItemInstance* Instance) const
2{
3 for (int32 i=0;i<InitialFloatAttributes.Num();i++)
4 {
5 Instance->SetFloatAttribute(InitialFloatAttributes[i].Tag,InitialFloatAttributes[i].Value);
6 }
7 for (int32 i=0;i<InitialIntegerAttributes.Num();i++)
8 {
9 Instance->SetIntegerAttribute(InitialIntegerAttributes[i].Tag,InitialIntegerAttributes[i].Value);
10 }
11 for (int32 i=0;i<InitialBoolAttributes.Num();i++)
12 {
13 Instance->SetBoolAttribute(InitialBoolAttributes[i].Tag,InitialBoolAttributes[i].Value);
14 }
15}

通过蓝图

道具定义.自定义片段
道具定义.使用自定义片段

访问道具定义数据

每一个道具实例创建时,都会持有对Definition的引用,并通过网络复制。Definition的数据都是只读的

当你拿到了道具实例的引用,你可以通过GetDefinition获取道具的所有静态信息,GIS也提供了便捷的函数供你访问各种Fragment数据。

管理大量道具定义

由于道具定义是数据资产,每一个文件代表着一个道具。在项目中道具数量非常庞大时,一个良好的文件夹结构将非常有助于对数据进行管理。

你应该将同类型的,即数据结构布局一致的道具定义放置在相同的文件夹内,这样你可以借助UE编辑器提供的矩阵编辑器来对文件夹内的所有道具进行批量编辑。

数据验证

当项目中的道具数量逐渐增长,且道具数据结构的设计逐渐趋于固定时,你可能会希望以统一的方式去约束某些类型的道具其数据结构布局,以减少人为错误。

GIS提供“道具定义模式(ItemDefinitionSchema)”,它是一个数据资产,包含一组以ItemTags进行匹配的验证规则。

道具定义.验证模式

当你在项目设置中指定了Schema后,每当ItemDefinition保存时,就会在此模式中查询匹配的规则,并进行数据校验,如果不满足规则,则会弹出编辑器警告以提示用户修正错误。

道具定义.指定验证模式



logo_small
罗传月武

© 罗传月武 @2025 版权所有.

道具定义 | 虚幻引擎 - 通用库存系统 | 月武的编程之旅