栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

UE5/C++ 基于GAS创建攻击伤害 5.4.2 创建角色属性

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UE5/C++ 基于GAS创建攻击伤害 5.4.2 创建角色属性

1.

创建继承至AttributeSet C++类,命名为MMOARPGAttributeSet

2. 创建属性定义通用宏

 MMOARPGGameplayAbilityMacros.h,它可以自动生成属性的所有样板代码

#pragma once

#include "AttributeSet.h"
//定义一个通用宏,用于定义属性集
#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) 
	GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) 
	GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) 
	GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) 
	GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)

3.属性创建,

打开MMOARPGAttributeSet.h

#pragma once

#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "AbilitySystemComponent.h"
#include "Net/UnrealNetwork.h"
#include "MMOARPGGameplayAbilityMacros.h"
#include "MMOARPGAttributeSet.generated.h"


UCLASS()
class MMOARPGGAME_API UMMOARPGAttributeSet : public UAttributeSet
{
	GENERATED_BODY()
public:
	UMMOARPGAttributeSet();

	virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override;


	virtual void PostGameplayEffectExecute(const struct FGameplayEffectModCallbackData& Data) override;

public:
	UPROPERTY(BlueprintReadOnly, Category = "Attribute", ReplicatedUsing = OnRep_Level)
	FGameplayAttributeData Level;//角色等级
	ATTRIBUTE_ACCESSORS(UMMOARPGAttributeSet, Level)

	UPROPERTY(BlueprintReadOnly, Category = "Attribute", ReplicatedUsing = OnRep_Health)
	FGameplayAttributeData Health;//生命值
	ATTRIBUTE_ACCESSORS(UMMOARPGAttributeSet, Health)

	UPROPERTY(BlueprintReadOnly, Category = "Attribute", ReplicatedUsing = OnRep_MaxHealth)
	FGameplayAttributeData MaxHealth;//最大生命值
	ATTRIBUTE_ACCESSORS(UMMOARPGAttributeSet, MaxHealth)

	UPROPERTY(BlueprintReadOnly, Category = "Attribute", ReplicatedUsing = OnRep_Mana)
	FGameplayAttributeData Mana;//魔法值
	ATTRIBUTE_ACCESSORS(UMMOARPGAttributeSet, Mana)

	UPROPERTY(BlueprintReadOnly, Category = "Attribute", ReplicatedUsing = OnRep_MaxMana)
	FGameplayAttributeData MaxMana;//最大魔法值
	ATTRIBUTE_ACCESSORS(UMMOARPGAttributeSet, MaxMana)


protected:
	UFUNCTION()
	virtual void OnRep_Level(const FGameplayAttributeData& OldValue);

	UFUNCTION()
	virtual void OnRep_Health(const FGameplayAttributeData& OldValue);

	UFUNCTION()
	virtual void OnRep_MaxHealth(const FGameplayAttributeData& OldValue);

	UFUNCTION()
	virtual void OnRep_Mana(const FGameplayAttributeData& OldValue);

	UFUNCTION()
	virtual void OnRep_MaxMana(const FGameplayAttributeData& OldValue);
};

MMOARPGAttributeSet.cpp的实现

#include "MMOARPGAttributeSet.h"
#include "GameplayEffect.h"
#include "GameplayEffectExtension.h"

UMMOARPGAttributeSet::UMMOARPGAttributeSet()
	:Level(1)
	, Health(100.f)
	, MaxHealth(100.f)
	, Mana(0.f)
	, MaxMana(0.f)
{

}

void UMMOARPGAttributeSet::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME(UMMOARPGAttributeSet, Level);
	DOREPLIFETIME(UMMOARPGAttributeSet, Health);
	DOREPLIFETIME(UMMOARPGAttributeSet, MaxHealth);
	DOREPLIFETIME(UMMOARPGAttributeSet, Mana);
	DOREPLIFETIME(UMMOARPGAttributeSet, MaxMana);
}

void UMMOARPGAttributeSet::PostGameplayEffectExecute(const struct FGameplayEffectModCallbackData& Data)
{
    //数值计算,例如受到伤害后扣血就是写在此处
}

void UMMOARPGAttributeSet::OnRep_Level(const FGameplayAttributeData& OldValue)
{
	GAMEPLAYATTRIBUTE_REPNOTIFY(UMMOARPGAttributeSet, Level, OldValue);
}

void UMMOARPGAttributeSet::OnRep_Health(const FGameplayAttributeData& OldValue)
{
	GAMEPLAYATTRIBUTE_REPNOTIFY(UMMOARPGAttributeSet, Health, OldValue);
}

void UMMOARPGAttributeSet::OnRep_MaxHealth(const FGameplayAttributeData& OldValue)
{
	GAMEPLAYATTRIBUTE_REPNOTIFY(UMMOARPGAttributeSet, MaxHealth, OldValue);
}

void UMMOARPGAttributeSet::OnRep_Mana(const FGameplayAttributeData& OldValue)
{
	GAMEPLAYATTRIBUTE_REPNOTIFY(UMMOARPGAttributeSet, Mana, OldValue);
}

void UMMOARPGAttributeSet::OnRep_MaxMana(const FGameplayAttributeData& OldValue)
{
	GAMEPLAYATTRIBUTE_REPNOTIFY(UMMOARPGAttributeSet, MaxMana, OldValue);
}

4.属性生成

打开Characterbase.h

class UMMOARPGAttributeSet;//前置声明

UPROPERTY(Category = MMOARPGCharacterbase, VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
TObjectPtrAttributeSet;

打开Characterbase.cpp,在构造函数中创建属性对象组件,在Beginplay中生成

AttributeSet = CreateDefaultSubobject(TEXT("AttributeSet"));
void AMMOARPGCharacterbase::BeginPlay()
{
	Super::BeginPlay();
	
	if (GetWorld())
	{

		//生成角色属性
		TArray AttributeSets;
		AttributeSets.Add(AttributeSet);
		AbilitySystemComponent->SetSpawnedAttributes(AttributeSets);
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664952.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号