书接 11. UE5 RPG使用GameplayEffect修改角色属性(二)
前面,介绍了GameplayEffect的Instant和Duration的使用,这一篇主要介绍一下无限制时间类型的infinite的使用方式。
无限时间限制模式下,如果你的周期时间(Period)为0,那就是相当于增加了一个状态加了一个Buff,效果是持续的,如果周期时间你设置了值,那么它将变成为每个周期执行一次Instant效果的GameplayEffect。
使用起来基本上和有时间限制的Duration一样,它们的区别在于,一个是有时间限制的,另一个是没有时间限制。
应用场景
- 可以使用到地面陷阱,比如地上的岩浆,角色站在上面会一直掉血,离开后,通过清除GameplayEffect来取消效果。
- 恢复水泉,角色站在旁边可以一直恢复。
- 无限制时间的buff,比如装备提供的buff,角色特有的属性bufff。
接下来,我们要制作一个通用的类,里面可以设置这三种属性的GameplayEffect,在蓝图里面,只需要设置何时触发效果即可。并且实现一下Infinite效果的添加和删除功能。
首先我们要添加两个枚举,用于设置添加的类在何时应用到actor身上
//效果应用状态枚举
UENUM(BlueprintType)
enum class EEffectApplicationPolicy
{
ApplyOnOverlap,
ApplyOnEndOverlap,
DoNotApply
};
//效果移除的状态枚举
UENUM(BlueprintType)
enum class EEffectRemovalPolicy
{
RemoveOnEndOverlap,
DoNotRemove
};
接着修改配置项的类,每一种GameplayEffect的类,Instant和Duration只有添加时的策略配置,(它们两个一个是瞬间效果,另一个是有时效性的,都不需要主动移除),Infinite的则需要设置添加策略和移除策略。
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
TSubclassOf<UGameplayEffect> InstantGameplayEffectClass; //生成GameplayEffect的类
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectApplicationPolicy InstantEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
TSubclassOf<UGameplayEffect> DurationGameplayEffectClass; //生成具有一定持续时间的GameplayEffect的类
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectApplicationPolicy DurationEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
TSubclassOf<UGameplayEffect> InfinityGameplayEffectClass; //生成具有一定持续时间的GameplayEffect的类
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectApplicationPolicy InfinityEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectRemovalPolicy InfinityEffectRemovalPolicy = EEffectRemovalPolicy::RemoveOnEndOverlap;
接着,我们需要创建两个函数,可以在蓝图中调用,触发overlap和endoverlap时的相关事件
//在重叠开始时处理效果的添加删除逻辑
UFUNCTION(BlueprintCallable)
void OnOverlap(AActor* TargetActor);
//在重叠结束时处理效果的添加删除逻辑
UFUNCTION(BlueprintCallable)
void OnEndOverlap(AActor* TargetActor);
在Overlap事件中,如果你设置了对应的需要在Overlap时添加效果,将去执行ApplyEffectToTarget()函数应用效果。
void AEffectActorBase::OnOverlap(AActor* TargetActor)
{
if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
{
ApplyEffectToTarget(TargetActor, InstantGameplayEffectClass);
}
if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
{
ApplyEffectToTarget(TargetActor, DurationGameplayEffectClass);
}
if(InfinityEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
{
ApplyEffectToTarget(TargetActor, InfinityGameplayEffectClass);
}
}
ApplyEffectToTarget()函数之前也讲过,这里有了更改,就是对于Infinite类型的效果并且在重叠结束时需要销毁的效果,我们需要将其的引用保存下来,因为它不会自己取消,需要手动去销毁。
void AEffectActorBase::ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass)
{
/**
* 默认自己编写从actor身上获取ASC的方式
* IAbilitySystemInterface* ASCInterface = Cast<IAbilitySystemInterface>(TargetActor); //判断当前actor是否有技能系统接口
if(ASCInterface)
{
UAbilitySystemComponent* TargetASC = ASCInterface->GetAbilitySystemComponent();
}
*/
//获取ASC
UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);
if(TargetASC == nullptr) return;
check(GameplayEffectClass);
//创建Effect的句柄 包含了实例化Effect所需数据
FGameplayEffectContextHandle EffectContextHandle = TargetASC->MakeEffectContext();
//设置创建Effect的对象
EffectContextHandle.AddSourceObject(this);
//Effect的实例化后的句柄,可以通过此来寻找调用
const FGameplayEffectSpecHandle EffectSpecHandle = TargetASC->MakeOutgoingSpec(GameplayEffectClass, 1.f, EffectContextHandle);
//从句柄中获取到实例的地址,并被应用。
const FActiveGameplayEffectHandle ActiveGameplayEffectHandle = TargetASC->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get());
//从句柄中获取到定义的对象,并判断设置的
const bool bIsInfinite = EffectSpecHandle.Data.Get()->Def.Get()->DurationPolicy == EGameplayEffectDurationType::Infinite;
//在是无限时间效果和需要在结束时清除掉时,将效果句柄添加到map
if(bIsInfinite && InfinityEffectRemovalPolicy == EEffectRemovalPolicy::RemoveOnEndOverlap)
{
ActiveEffectHandles.Add(ActiveGameplayEffectHandle, TargetASC);
}
}
在结束重叠事件函数中,首先逻辑判断是否需要在离开时添加效果。接下来就是判断Infinite类型的效果是否需要在结束重叠事件时移除,如果需要则通过ASC判断相同来找到对应的效果进行移除,在堆栈里面还不能直接移除,所以先将引用保存了下来
void AEffectActorBase::OnEndOverlap(AActor* TargetActor)
{
//添加效果
if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnEndOverlap)
{
ApplyEffectToTarget(TargetActor, InstantGameplayEffectClass);
}
if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnEndOverlap)
{
ApplyEffectToTarget(TargetActor, DurationGameplayEffectClass);
}
if(InfinityEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnEndOverlap)
{
ApplyEffectToTarget(TargetActor, InfinityGameplayEffectClass);
}
//删除效果
if(InfinityEffectRemovalPolicy == EEffectRemovalPolicy::RemoveOnEndOverlap)
{
UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);
if(!IsValid(TargetASC)) return;
//创建存储需要移除的效果句柄存储Key,用于遍历完成后移除效果
TArray<FActiveGameplayEffectHandle> HandlesToRemove;
//循环map内存的数据
for(TTuple<FActiveGameplayEffectHandle, UAbilitySystemComponent*> HandlePair : ActiveEffectHandles)
{
//判断是否ASC相同
if(TargetASC == HandlePair.Value)
{
//通过句柄将效果移除,注意,有可能有多层效果,不能将其它层的效果也移除掉,所以只移除一层
TargetASC->RemoveActiveGameplayEffect(HandlePair.Key, 1);
//添加到移除列表
HandlesToRemove.Add(HandlePair.Key);
}
}
//遍历完成后,在Map中将移除效果的KeyValue删除
for(auto& Handle : HandlesToRemove)
{
ActiveEffectHandles.FindAndRemoveChecked(Handle);
}
}
}
接着编译打开ue,看到右侧有对应的属性配置
事件图表更好设置,在对应的碰撞体事件触发回调时链接即可
GameplayEffect的设置就很简单了,一个持续掉血的效果
接下来就是测试,运行起来,点击~键,然后输入showdebug abilitysystem 来查看掉血情况
下面附上上面两个文件的整个代码:
EffectActorBase.h
// 版权归暮志未晚所有。
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GameplayEffectTypes.h"
#include "EffectActorBase.generated.h"
struct FActiveGameplayEffectHandle;
class UAbilitySystemComponent;
class UGameplayEffect;
//效果应用状态枚举
UENUM(BlueprintType)
enum class EEffectApplicationPolicy
{
ApplyOnOverlap,
ApplyOnEndOverlap,
DoNotApply
};
//效果移除的状态枚举
UENUM(BlueprintType)
enum class EEffectRemovalPolicy
{
RemoveOnEndOverlap,
DoNotRemove
};
/**
* 在场景中可放置的影响角色属性的物件基类
*/
UCLASS()
class AURA_API AEffectActorBase : public AActor
{
GENERATED_BODY()
public:
AEffectActorBase();
protected:
// 游戏开始或生成对象时回调
virtual void BeginPlay() override;
//给与目标添加GameplayEffect效果
UFUNCTION(BlueprintCallable)
void ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass);
//在重叠开始时处理效果的添加删除逻辑
UFUNCTION(BlueprintCallable)
void OnOverlap(AActor* TargetActor);
//在重叠结束时处理效果的添加删除逻辑
UFUNCTION(BlueprintCallable)
void OnEndOverlap(AActor* TargetActor);
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
TSubclassOf<UGameplayEffect> InstantGameplayEffectClass; //生成GameplayEffect的类
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectApplicationPolicy InstantEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
TSubclassOf<UGameplayEffect> DurationGameplayEffectClass; //生成具有一定持续时间的GameplayEffect的类
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectApplicationPolicy DurationEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
TSubclassOf<UGameplayEffect> InfinityGameplayEffectClass; //生成具有一定持续时间的GameplayEffect的类
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectApplicationPolicy InfinityEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Apply Effects")
EEffectRemovalPolicy InfinityEffectRemovalPolicy = EEffectRemovalPolicy::RemoveOnEndOverlap;
//用于存储当前已经激活的GameplayEffect的句柄的map
TMap<FActiveGameplayEffectHandle, UAbilitySystemComponent*> ActiveEffectHandles;
};
EffectActorBase.cpp
// 版权归暮志未晚所有。
#include "Actor/EffectActorBase.h"
#include "ActiveGameplayEffectHandle.h"
#include "AbilitySystemBlueprintLibrary.h"
#include "AbilitySystemComponent.h"
AEffectActorBase::AEffectActorBase()
{
// 设置当前对象是否每帧调用Tick()
PrimaryActorTick.bCanEverTick = false;
SetRootComponent(CreateDefaultSubobject<USceneComponent>("SceneRoot"));
}
void AEffectActorBase::BeginPlay()
{
Super::BeginPlay();
}
void AEffectActorBase::ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass)
{
/**
* 默认自己编写从actor身上获取ASC的方式
* IAbilitySystemInterface* ASCInterface = Cast<IAbilitySystemInterface>(TargetActor); //判断当前actor是否有技能系统接口
if(ASCInterface)
{
UAbilitySystemComponent* TargetASC = ASCInterface->GetAbilitySystemComponent();
}
*/
//获取ASC
UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);
if(TargetASC == nullptr) return;
check(GameplayEffectClass);
//创建Effect的句柄 包含了实例化Effect所需数据
FGameplayEffectContextHandle EffectContextHandle = TargetASC->MakeEffectContext();
//设置创建Effect的对象
EffectContextHandle.AddSourceObject(this);
//Effect的实例化后的句柄,可以通过此来寻找调用
const FGameplayEffectSpecHandle EffectSpecHandle = TargetASC->MakeOutgoingSpec(GameplayEffectClass, 1.f, EffectContextHandle);
//从句柄中获取到实例的地址,并被应用。
const FActiveGameplayEffectHandle ActiveGameplayEffectHandle = TargetASC->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get());
//从句柄中获取到定义的对象,并判断设置的
const bool bIsInfinite = EffectSpecHandle.Data.Get()->Def.Get()->DurationPolicy == EGameplayEffectDurationType::Infinite;
//在是无限时间效果和需要在结束时清除掉时,将效果句柄添加到map
if(bIsInfinite && InfinityEffectRemovalPolicy == EEffectRemovalPolicy::RemoveOnEndOverlap)
{
ActiveEffectHandles.Add(ActiveGameplayEffectHandle, TargetASC);
}
}
void AEffectActorBase::OnOverlap(AActor* TargetActor)
{
if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
{
ApplyEffectToTarget(TargetActor, InstantGameplayEffectClass);
}
if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
{
ApplyEffectToTarget(TargetActor, DurationGameplayEffectClass);
}
if(InfinityEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
{
ApplyEffectToTarget(TargetActor, InfinityGameplayEffectClass);
}
}
void AEffectActorBase::OnEndOverlap(AActor* TargetActor)
{
//添加效果
if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnEndOverlap)
{
ApplyEffectToTarget(TargetActor, InstantGameplayEffectClass);
}
if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnEndOverlap)
{
ApplyEffectToTarget(TargetActor, DurationGameplayEffectClass);
}
if(InfinityEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnEndOverlap)
{
ApplyEffectToTarget(TargetActor, InfinityGameplayEffectClass);
}
//删除效果
if(InfinityEffectRemovalPolicy == EEffectRemovalPolicy::RemoveOnEndOverlap)
{
UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);
if(!IsValid(TargetASC)) return;
//创建存储需要移除的效果句柄存储Key,用于遍历完成后移除效果
TArray<FActiveGameplayEffectHandle> HandlesToRemove;
//循环map内存的数据
for(TTuple<FActiveGameplayEffectHandle, UAbilitySystemComponent*> HandlePair : ActiveEffectHandles)
{
//判断是否ASC相同
if(TargetASC == HandlePair.Value)
{
//通过句柄将效果移除,注意,有可能有多层效果,不能将其它层的效果也移除掉,所以只移除一层
TargetASC->RemoveActiveGameplayEffect(HandlePair.Key, 1);
//添加到移除列表
HandlesToRemove.Add(HandlePair.Key);
}
}
//遍历完成后,在Map中将移除效果的KeyValue删除
for(auto& Handle : HandlesToRemove)
{
ActiveEffectHandles.FindAndRemoveChecked(Handle);
}
}
}