这一部分学习了怎么创建一个伤害性的地形(火焰地形,毒沼泽等都可以用这个方式创建)
AuraEffectActor.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameplayEffect.h"
#include "AbilitySystem/AuraAbilitySystemComponentBase.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Actor.h"
#include "GameplayEffectTypes.h"
#include "AuraEffectActor.generated.h"class UGameplayEffect;
class UAbilitySystemComponent;UENUM(BlueprintType)
enum class EEffectApplicationPolicy : uint8 { ApplyOnOverlap,ApplyOnEndOverlap,DoNotApply };UENUM(BlueprintType)
enum class EEffectRemovePolicy : uint8 { RemoveOnEndOverlap,DoNotRemove };UCLASS()
class MYGAS_API AAuraEffectActor : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAAuraEffectActor();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;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="Applied Effects")TSubclassOf<UGameplayEffect> InstantGameplayEffectClass;UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")EEffectApplicationPolicy InstantEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")TSubclassOf<UGameplayEffect> DurationGameplayEffectClass;UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")EEffectApplicationPolicy DurationEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")TSubclassOf<UGameplayEffect> InfiniteGameplayEffectClass;UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")EEffectApplicationPolicy InfiniteEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")EEffectRemovePolicy InfiniteEffectRemovePolicy = EEffectRemovePolicy::RemoveOnEndOverlap;TMap<FActiveGameplayEffectHandle , UAbilitySystemComponent*> ActiveEffectHandles;
private:};
AuraEffectActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "Actor/AuraEffectActor.h"#include "AbilitySystemBlueprintLibrary.h"
#include "AbilitySystemComponent.h"
#include "AbilitySystemInterface.h"
#include "AbilitySystem/AuraAbilitySystemComponentBase.h"
#include "AbilitySystem/AuraAttributeSet.h"
#include "Components/StaticMeshComponent.h"// Sets default values
AAuraEffectActor::AAuraEffectActor()
{// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = false;SetRootComponent(CreateDefaultSubobject<USceneComponent>("SceneRoot"));
}// Called when the game starts or when spawned
void AAuraEffectActor::BeginPlay()
{Super::BeginPlay();}void AAuraEffectActor::ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass)
{//这里是使用 UAbilitySystemBlueprintLibrary 内的静态函数,在指定的Target上查找并返回UAbilitySystemComponentUAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);if(TargetASC==nullptr) return;check(GameplayEffectClass);//创建了一个游戏效果的内容句柄,提供游戏效果的信息FGameplayEffectContextHandle TargetASCContext =TargetASC->MakeEffectContext();//把自己作为游戏效果的源对象,确保AbilitySystem可以识别到自己TargetASCContext.AddSourceObject(this);//这里创建了一个游戏效果规范,GameplayEffectClass 是要应用的游戏效果的类,1.0f 是游戏效果的初始生命周期倍率,TargetASCContext 是游戏效果的上下文. 调用 MakeOutgoingSpec 函数,将会根据提供的参数创建一个游戏效果规范const FGameplayEffectSpecHandle EffectSpecHandle = TargetASC->MakeOutgoingSpec(GameplayEffectClass,1.0f,TargetASCContext);const FActiveGameplayEffectHandle ActiveGameplayEffectHandle = TargetASC->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get());const bool bIsInfinite = EffectSpecHandle.Data.Get()->Def.Get()->DurationPolicy == EGameplayEffectDurationType::Infinite;if(bIsInfinite){ActiveEffectHandles.Add(ActiveGameplayEffectHandle,TargetASC);}
}void AAuraEffectActor::OnOverlap(AActor* TargetActor)
{if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap){ApplyEffectToTarget(TargetActor,InstantGameplayEffectClass);}if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap){ApplyEffectToTarget(TargetActor,DurationGameplayEffectClass);}if(InfiniteEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap){ApplyEffectToTarget(TargetActor,InfiniteGameplayEffectClass);}
}void AAuraEffectActor::OnEndOverlap(AActor* TargetActor)
{if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap){ApplyEffectToTarget(TargetActor,InstantGameplayEffectClass);}if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap){ApplyEffectToTarget(TargetActor,DurationGameplayEffectClass);}if(InfiniteEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap){ApplyEffectToTarget(TargetActor,InfiniteGameplayEffectClass);}if(InfiniteEffectRemovePolicy == EEffectRemovePolicy::RemoveOnEndOverlap){//获取到目标角色的能力组件UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);if(!IsValid(TargetASC)) return;//创建了一个储存移除游戏效果句柄的数组 HandlesToRemoveTArray<FActiveGameplayEffectHandle> HandlesToRemove;//遍历 ActiveEffectHandles 数组for(TTuple<FActiveGameplayEffectHandle, UAbilitySystemComponent*> HandlePair:ActiveEffectHandles){// 当 TargetASC 内有 HandlePair.Value的值的时候if(TargetASC == HandlePair.Value){// 移除 HandlePair 的效果TargetASC->RemoveActiveGameplayEffect(HandlePair.Key, 1);//把这个效果放在 HandlesToRemove 中HandlesToRemove.Add(HandlePair.Key);}}// 遍历 HandlesToRemove 内的每一个元素for(auto& Handle : HandlesToRemove){// 在 ActiveEffectHandles 这个数组内移除ActiveEffectHandles.FindAndRemoveChecked(Handle);}}}
创建GE_FireArea和BP_FireArea
FireArea需要把Applied Effects设置喂Apply Overlay