GAS-Aura 课程笔记02
三、Intro to the Gameplay Ability System
15.游戏能力系统
-
介绍属性,技能,增益,debuff


16.GAS主要部件
-
ASC,AS,GA,AT,GE,GC,Game Tag

-
ASC和AS的两种添加方法:添加到Pawn或者PlayerState
-
如果放在Pawn上,如果Pawn死了,ASC和AS就没了,重新生成角色那些数据是新的。
-
如果放在PlayerState上,就没有这个问题,可以切换Pawn。
-
本项目的添加方法:

-
所以接下来要创建一些PlayerState类,ASC类,AS类
-
17.玩家状态
- 在Player文件夹下创建
AuraPlayerState类,继承PlayerState C++类 - 创建构造函数,设置更新频率为100,
NetupdateFrequency = 100.f; - 创建BP_AuraPlayerState,继承
AuraPlayerState - 在GM中设置PS
18.能力系统组件和属性集
- 插件中启用GAS:
Gameplay Abilities - 创建C++类,继承
AbilitySystemComponent,名为AuraAbilitySystemComponent,目录为/public/AbilitySystem - 创建C++类,继承
AttributeSet,名为AuraAttributeSet,目录为/public/AbilitySystem - 在
.Build.cs文件添加需要的模块Private:"GameplayAbilities","GameplayTags","GameplayTasks"
19.多人游戏中的GAS
-
介绍


-
注意:每个客户端都有自己的HUD类,并且仅在该客户端上存在,所以专有服务器上没用HUD,监听服务器上的HUD是该本地玩家的HUD
-
变量复制只在服务器向客户端复制。而客户端想到服务器就只有用RPC了
20.构建能力系统组件和属性集
-
AuraCharacterBase中创建T对象指针,ASC和AS。但是因为只在敌人角色中有这两个,而玩家角色需要放在PlayerState中,所以要到敌人中创建。class UAbilitySystemComponent; class UAttributeSet; UPROPERTY() TObjectPtr<UAbilitySystemComponent> AbilitySystemComponent; UPROPERTY() TObjectPtr<UAttributeSet> AttributeSet;-
继承
IAbilitySystemInterface接口,该接口有个获取能力系统组件的纯虚函数GetAbilitySystemComponent(),我们实现并用来验证或获取能力系统组件virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override; UAbilitySystemComponent* AAuraCharacterBase::GetAbilitySystemComponent() const { return AbilitySystemComponent; } -
创建获取属性集的函数
GetAttributeSet()UAttributeSet* GetAttributeSet() const{return AttributeSet;} -
在
AuraEnemy中将父类声明的两个指针变量赋值:创建ASC并设置为复制。创建AS。注意创建的是Aura开头的ASC和ASAbilitySystemComponent = CreateDefaultSubobject<UAuraAbilitySystemComponent>(TEXT("AbilitySystemComponent")); AbilitySystemComponent->SetIsReplicated(true); AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);//Minimal适用于AI控制的角色 AttributeSet = CreateDefaultSubobject<UAuraAttributeSet>(TEXT("AttributeSet"));
-
-
在
AuraPlayerState中声明这两个变量并创建,实现IAbilitySystemInterface接口的纯虚函数,和创建GetAttributeSet()函数.同上
21.复制模式
-
设置
AuraPlayerState和EnemyBase类上的ASC的复制模式。- 经验而谈:玩家控制的设置为
Mixed,AI控制的设置为Minimal
AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed); - 经验而谈:玩家控制的设置为
-
GE的复制模式

-
GE是什么,是对玩家造成伤害或治疗类似的
22.初始化ASC的拥有者和代表着
-
介绍

-
什么时候调用呢
- 如图

-
在AuraCharacter中创建私有函数,用于初始化ASC拥有者和代表者,同时在玩家角色类中赋值ASC和AS指针。
oid AAuraCharacter::PossessedBy(AController* NewController) { Super::PossessedBy(NewController); // 在服务器中初始化AbilityActorInfo InitAbilityActorInfo(); } void AAuraCharacter::OnRep_PlayerState() { Super::OnRep_PlayerState(); // 在客户端中初始化AbilityActorInfo InitAbilityActorInfo(); } void AAuraCharacter::InitAbilityActorInfo() { AAuraPlayerState* AuraPlayerState = GetPlayerState<AAuraPlayerState>(); check(AuraPlayerState); AuraPlayerState->GetAbilitySystemComponent()->InitAbilityActorInfo(AuraPlayerState,this); AbilitySystemComponent = AuraPlayerState->GetAbilitySystemComponent(); AttributeSet = AuraPlayerState->GetAttributeSet(); } -
注意一点:

对于混合复制模式:ASC的OwnerActor的所有者必须是控制者。
对于Pawn,这在PossessedBy()中自动设置 对于玩家状态,它的所有者自动设置为控制器。
如果是其他,需要手动设置ASC的OwnerActor的所有者为控制器
四、Attributes
23.属性
-
属性集AS可以有多个,但是该项目只使用一个。
-
预测:相当于客户端不需要等到服务器确认更改,先自行更改,然后再通知服务器,服务器再判断是否有效,无效的话就让客户端回滚操作



-
什么是属性值:由基础值和当前值构成,当前值代表临时值

24.生命和魔法
-
AuraAttributeSet类添加构造函数
- 添加生命属性
public: UAuraAttributeSet(); //获取生命周期中需要复制的属性 virtual void GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const override; //该变量的值发生变化时,调用OnRep_Health函数 UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_Health,Category = "Vital Attributes") FGameplayAttributeData Health; UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_MaxHealth,Category = "Vital Attributes") FGameplayAttributeData MaxHealth; UFUNCTION() void OnRep_Health(const FGameplayAttributeData& OldHealth) const; UFUNCTION() void OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const;void UAuraAttributeSet::GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); //Health属性的复制条件为:COND_None,意味着不需要复制条件,只要属性发生变化就会复制,REPNOTIFY_Always表示属性发生变化时总是会调用OnRep_Health函数 DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet, Health, COND_None, REPNOTIFY_Always); DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet, MaxHealth, COND_None, REPNOTIFY_Always); } void UAuraAttributeSet::OnRep_Health(const FGameplayAttributeData& OldHealth) const { // 这是一个辅助宏,可以在RepNotify函数中使用,以处理客户端可能会预测性修改的属性。 // OldHealth是属性变化之前的值,Health是变化之后的值,旧值用于回滚。 GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet, Health, OldHealth); } void UAuraAttributeSet::OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const { GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet, MaxHealth, OldMaxHealth); }- 上面添加属性的步骤是模板步骤,继续添加魔法和最大魔法属性
25.属性访问器
-
将.cpp中引入的ASC,剪切到.h中,因为要使用属性访问器
-
AS源代码中到底属性访问器宏的定义
/** * 这定义了一组用于访问和初始化属性的辅助函数,以避免手动编写这些函数。 * 它会为属性 Health 创建如下函数: * * static FGameplayAttribute UMyHealthSet::GetHealthAttribute(); * FORCEINLINE float UMyHealthSet::GetHealth() const; * FORCEINLINE void UMyHealthSet::SetHealth(float NewVal); * FORCEINLINE void UMyHealthSet::InitHealth(float NewVal); * * 在你的游戏中使用这些函数时,你可以这样定义,并根据需要添加游戏特定的函数: * * #define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \ * GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \ * GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \ * GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \ * GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName) * * ATTRIBUTE_ACCESSORS(UMyHealthSet, Health) */ -
在
AuraAttributeSet中定义该宏,并在构造函数中调用InitHealth函数设置默认值 -
验证修改:showdebug abilitysystem
-
重复上面做法,将剩下属性也设置
26.效果Actor
-
创建继承
Actor的C++类,AuraEffectActor,目录public/Actor- 删除tick函数,设置tick为false;
- 创建私有变量,球体组件Sphere
- 构造函数中为Sphere创建对象,并附加到根组件
- 创建私有变量,静态网格组件Mesh
- 构造函数总为Mesh创建对象,并设置为根组件。
- 创建公共函数,重写OnOverlap重叠函数
- BeginPlay中设置Sphere组件重叠委托,绑定为OnOverlap函数
- 创建公共函数,重写EndOverlap结束重叠函数。
- BeginPlay中设置Sphere组件结束重叠委托,绑定到EndOverlap函数
- 编写代码实现改变属性
- Tip:如何快速创建委托的回调函数呢,在蓝图中我们可以快速创建,并帮我们把参数设置好了。但在C++中,我们直接去Sphere->
OnComponentBeginOverlap.AddDynamic,委托里面复制声明的委托参数,然后删掉不需要的委托变量名和不需要的逗号。
- Tip:如何快速创建委托的回调函数呢,在蓝图中我们可以快速创建,并帮我们把参数设置好了。但在C++中,我们直接去Sphere->
// Learn by Zmingu #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "AuraEffectActor.generated.h" class USphereComponent; UCLASS() class AURA_API AAuraEffectActor : public AActor { GENERATED_BODY() public: AAuraEffectActor(); UFUNCTION() virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); UFUNCTION() virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); protected: virtual void BeginPlay() override; private: UPROPERTY(VisibleAnywhere) TObjectPtr<UStaticMeshComponent> Mesh; UPROPERTY(VisibleAnywhere) TObjectPtr<USphereComponent> Sphere; };// Learn by Zmingu #include "Actor/AuraEffectActor.h" #include "AbilitySystemComponent.h" #include "AbilitySystemInterface.h" #include "AbilitySystem/AuraAttributeSet.h" #include "Components/SphereComponent.h" // Sets default values AAuraEffectActor::AAuraEffectActor() { PrimaryActorTick.bCanEverTick = false; Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh"); SetRootComponent(Mesh); Sphere = CreateDefaultSubobject<USphereComponent>("Sphere"); Sphere->SetupAttachment(RootComponent); } void AAuraEffectActor::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { //T0D0: Change this to apply a Gameplay Effect. For now, using const_cast as a hack! // 如果重叠的Actor实现了IAbilitySystemInterface接口 if (IAbilitySystemInterface* AbilitySystemInterface = Cast<IAbilitySystemInterface>(OtherActor)) { const UAuraAttributeSet* AuraAttributeSet = Cast<UAuraAttributeSet>(AbilitySystemInterface->GetAbilitySystemComponent()->GetAttributeSet(UAuraAttributeSet::StaticClass())); UAuraAttributeSet* MutableAttributeSet = const_cast<UAuraAttributeSet*>(AuraAttributeSet); MutableAttributeSet->SetHealth(AuraAttributeSet->GetHealth() + 10.f); Destroy(); } } void AAuraEffectActor::EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) { } void AAuraEffectActor::BeginPlay() { Super::BeginPlay(); Sphere->OnComponentBeginOverlap.AddDynamic(this, &AAuraEffectActor::OnOverlap); Sphere->OnComponentEndOverlap.AddDynamic(this,&AAuraEffectActor::EndOverlap); }