一、创建新的C++类,继承自AttributeSet
TestAttributeSet.h
#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "BaseAttributeSet.generated.h"
UCLASS()
class GASINTRO_API UTestAttributeSet : public UAttributeSet
{
GENERATED_BODY()
public:
UTestAttributeSet();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TestAttributeSet")
FGameplayAttributeData MaxHealth;//最大生命值
};
TestAttributeSet.cpp
UBaseAttributeSet::UBaseAttributeSet():
MaxHealth(100.0f),
{
}
注意:FGameplayAttributeData类型在概述里Attribute有讲过,提供BaseValue与CurrentValue
二、将创建好的TestAttributeSet在角色C++类中声明一个指针
TestCharacter.h
class GASINTRO_API ATestCharacter : public ACharacter, public IAbilitySystemInterface
{
GENERATED_BODY()
public:
ABaseCharacter();
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "TestCharacter")
class UTestAttributeSet* AttributeSet;
}
TestCharacter.cpp
#include "项目名/Gameplay/TestAttributeSet.h"
ATestCharacter::ATestCharacter()
{
AttributeSet = CreateDefaultSubobject<UTestAttributeSet>("AttributeSet");
}
编译之后可在角色蓝图里看到生成了属性的结构体