PrimaryDataAsset
请先阅读,官方文档:资源管理
/**
* A DataAsset that implements GetPrimaryAssetId and has asset bundle support, which makes it something that can be manually loaded/unloaded from the AssetManager
* Making blueprint subclasses of this is useful because you can make blueprint-only primary asset types
* Blueprint subclasses will end up with a PrimaryAssetType equal to the name of the first native class found going up the hierarchy, or the top level blueprint class that directly subclasses this
* IE, if you have PrimaryDataAsset -> ParentNativeClass -> ChildNativeClass -> BlueprintAsset the type will be ChildNativeClass
* Whereas if you have PrimaryDataAsset -> ParentBlueprintClass -> ChildBlueprintClass the type will be ParentBlueprintClass
* To override this behavior, override GetPrimaryAssetId in your native class
*/
PrimaryDataAsset
为我们默认实现了继承自UObject
的GetPrimaryAssetId()
方法,并提供了asset bundle支持。
RPGItem.h/cpp
主要包含Item各种属性:
- ItemType : 用于Primary Asset 资源管理
- ItemName: 玩家看到的道具名
- ItemDiscription: 道具说明文字
- ItemIcon: 道具图标
- Price: 道具价格
- MaxCount: 道具在背包中的最大值(本游戏中,1代表不可消耗:如武器、技能,<=0代表无穷,如药水)
- MaxLevel:道具的最大等级
- GrantedAbility:使用道具将会获得的技能
- AbilityLevel:技能等级
注意:当我们用蓝图拓展这个类时,只能修改属性,不能拓展方法。
头文件:
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "ActionRPG.h"
#include "Engine/DataAsset.h"
#include "Styling/SlateBrush.h"
#include "RPGAssetManager.h"
#include "RPGItem.generated.h"
class URPGGameplayAbility;
/** Base class for all items, do not blueprint directly */
UCLASS(Abstract, BlueprintType)
class ACTIONRPG_API URPGItem : public UPrimaryDataAsset
{
GENERATED_BODY()
public:
/** Constructor */
URPGItem()
: Price(0)
, MaxCount(1)
, MaxLevel(1)
, AbilityLevel(1)
{}
/** Type of this item, set in native parent class */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Item)
FPrimaryAssetType ItemType;
/** User-visible short name */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FText ItemName;
/** User-visible long description */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FText ItemDescription;
/** Icon to display */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FSlateBrush ItemIcon;
/** Price in game */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
int32 Price;
/** Maximum number of instances that can be in inventory at once, <= 0 means infinite */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Max)
int32 MaxCount;
/** Returns if the item is consumable (MaxCount <= 0)*/
UFUNCTION(BlueprintCallable, BlueprintPure, Category = Max)
bool IsConsumable() const;
/** Maximum level this item can be, <= 0 means infinite */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Max)
int32 MaxLevel;
/** Ability to grant if this item is slotted */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Abilities)
TSubclassOf GrantedAbility;
/** Ability level this item grants. <= 0 means the character level */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Abilities)
int32 AbilityLevel;
/** Returns the logical name, equivalent to the primary asset id */
UFUNCTION(BlueprintCallable, Category = Item)
FString GetIdentifierString() const;
/** Overridden to use saved type */
virtual FPrimaryAssetId GetPrimaryAssetId() const override;
};
源文件:
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "Items/RPGItem.h"
bool URPGItem::IsConsumable() const
{
if (MaxCount <= 0)
{
return true;
}
return false;
}
FString URPGItem::GetIdentifierString() const
{
return GetPrimaryAssetId().ToString();
}
FPrimaryAssetId URPGItem::GetPrimaryAssetId() const
{
// This is a DataAsset and not a blueprint so we can just use the raw FName
// For blueprints you need to handle stripping the _C suffix
return FPrimaryAssetId(ItemType, GetFName());
}
其他继承自URPGItem
的类有URPGPotionItem,URPGSkillItem,URPGTokenItem,URPGWeaponItem
。这些子类主要是在构造函数中改变ItemType
的值。武器还添加了一个TSubclassOf
来指明武器对应的actor。