虚幻引擎5 GAS开发俯视角RPG游戏 P02-10 生命值与魔法值

Attributes 是由 FGameplayAttributeData定义的浮点值。 Attributes能够表达从角色的生命值到角色等级到药瓶的价格等任何数值。 如果Actor拥有游戏性相关的数值,那么可以考虑使用Attribute。Attributes 通常只能被GameplayEffects 修改,因此ASC可以 预测 这个修改。

定义AttributeSet属性

在之前的文章中,我创建了AS的文件,里面没有添加角色属性。
这里我先创建了两个属性,一个是Health,用来表示角色的当前血量,另一个是MaxHealth,代表角色的最大血量。

1.创建FGameplayAttributeData类型的变量Health

	UPROPERTY(BlueprintReadOnly, Category="Vital Attributes")
	FGameplayAttributeData Health;

2.要想属性被复制,首先在属性集标头的属性定义中加入ReplicatedUsingSpecifier,这将设置一个回调函数,有助于在远程系统上进行预测。

3.声明复制回调函数:

	void OnRep_Health(FGameplayAttributeData& OldHealth);

这时,上面的红线还在,提示找不到回调函数,所以,必须加上:UFUNCTION()

4.在属性集的源文件中,定义你复制的回调函数。函数的主体可以用游戏玩法技能系统定义的一个宏来表示。

// 使用默认的游戏玩法属性系统更新通知行为。
         GAMEPLAYATTRIBUTE_REPNOTIFY(UCCAttributeSet, Health, OldHealth);

所以在CCAttributeSet.cpp文件里,定义方法:

#include "AbilitySystemComponent.h"

void UCCAttributeSet::OnRep_Health(const FGameplayAttributeData& OldHealth) const
{
	// 使用默认的游戏玩法属性系统更新通知行为。
	GAMEPLAYATTRIBUTE_REPNOTIFY(UCCAttributeSet, Health, OldHealth);
}

5.如果这是你的属性集中的首个复制的属性,你要对公共的GetLifetimeReplicatedProps函数设置一个覆盖。

	     /** Marks the properties we wish to replicate */
     virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

6.将游戏玩法属性添加到属性集源文件中的GetLifetimeReplicatedProps函数中,如下所示:

void UCCAttributeSet::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	// 为生命值添加复制功能。
	DOREPLIFETIME_CONDITION_NOTIFY(UCCAttributeSet, Health, COND_None, REPNOTIFY_Always);
}

完成!!!

后面再添加最大血量、魔法值、最大魔法值

CCAttributeSet.h:

// 版权归陈超所有

#pragma once

#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "CCAttributeSet.generated.h"

/**
 * 
 */
UCLASS()
class CC_AURA_API UCCAttributeSet : public UAttributeSet
{
	GENERATED_BODY()

public:
	UCCAttributeSet();

	/** 标记我们希望复制的属性
	 * 如果这是你的属性集中的首个复制的属性,你要对公共的GetLifetimeReplicatedProps函数设置一个覆盖。
	 */
	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
	
/**************************************************************************************************************/
	//~Begin 属性
	UPROPERTY(BlueprintReadOnly, Category="Vital Attributes", ReplicatedUsing = OnRep_Health)
	FGameplayAttributeData Health;
	
	UPROPERTY(BlueprintReadOnly, Category="Vital Attributes", ReplicatedUsing = OnRep_MaxHealth)
	FGameplayAttributeData MaxHealth;

	UPROPERTY(BlueprintReadOnly, Category="Vital Attributes", ReplicatedUsing = OnRep_Mana)
	FGameplayAttributeData Mana;
	
	UPROPERTY(BlueprintReadOnly, Category="Vital Attributes", ReplicatedUsing = OnRep_MaxMana)
	FGameplayAttributeData MaxMana;

	//~End 属性
/**************************************************************************************************************/
	//~Begin 属性复制回调函数
	UFUNCTION()
	void OnRep_Health(const FGameplayAttributeData& OldHealth) const;

	UFUNCTION()
	void OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const;

	UFUNCTION()
	void OnRep_Mana(const FGameplayAttributeData& OldMana) const;

	UFUNCTION()
	void OnRep_MaxMana(const FGameplayAttributeData& OldMaxMana) const;

	//~End 属性复制回调函数
/**************************************************************************************************************/
};

CCAttributeSet.cpp:

// 版权归陈超所有


#include "AbilitySystem/CCAttributeSet.h"

#include "AbilitySystemComponent.h"
#include "Net/UnrealNetwork.h"

UCCAttributeSet::UCCAttributeSet()
{
}

//将游戏玩法属性添加到属性集源文件中的GetLifetimeReplicatedProps函数中
void UCCAttributeSet::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	
	// 为生命值添加复制功能。
	DOREPLIFETIME_CONDITION_NOTIFY(UCCAttributeSet, Health, COND_None, REPNOTIFY_Always);
	// 为最大生命值添加复制功能。
	DOREPLIFETIME_CONDITION_NOTIFY(UCCAttributeSet, MaxHealth, COND_None, REPNOTIFY_Always);
	// 为魔法值添加复制功能。
	DOREPLIFETIME_CONDITION_NOTIFY(UCCAttributeSet, Mana, COND_None, REPNOTIFY_Always);
	// 为最大魔法值添加复制功能。
	DOREPLIFETIME_CONDITION_NOTIFY(UCCAttributeSet, MaxMana, COND_None, REPNOTIFY_Always);
}



//~Begin 属性复制回调函数
void UCCAttributeSet::OnRep_Health(const FGameplayAttributeData& OldHealth) const
{
	// 使用默认的游戏玩法属性系统更新通知行为。
	GAMEPLAYATTRIBUTE_REPNOTIFY(UCCAttributeSet, Health, OldHealth);
}

void UCCAttributeSet::OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const
{
	// 使用默认的游戏玩法属性系统更新通知行为。
	GAMEPLAYATTRIBUTE_REPNOTIFY(UCCAttributeSet, MaxHealth, OldMaxHealth);
}

void UCCAttributeSet::OnRep_Mana(const FGameplayAttributeData& OldMana) const
{
	// 使用默认的游戏玩法属性系统更新通知行为。
	GAMEPLAYATTRIBUTE_REPNOTIFY(UCCAttributeSet, Mana, OldMana);
}

void UCCAttributeSet::OnRep_MaxMana(const FGameplayAttributeData& OldMaxMana) const
{
	// 使用默认的游戏玩法属性系统更新通知行为。
	GAMEPLAYATTRIBUTE_REPNOTIFY(UCCAttributeSet, MaxMana, OldMaxMana);
}

//~End 属性复制回调函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值