UE5 GAS开发P35,36,37,38,39 将药水修改为AbilitySystem效果

发布于:2024-04-25 ⋅ 阅读:(21) ⋅ 点赞:(0)

这几节课都是将药水修改成更方便使用的AbilitySystem效果的Actor,分别为增加血量,增加蓝量,暂时获得最大生命值上限

AuraEffectActor.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameplayEffect.h" #include "Components/SphereComponent.h" #include "GameFramework/Actor.h" #include "AuraEffectActor.generated.h" class UGameplayEffect; UCLASS() class MYGAS_API AAuraEffectActor : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AAuraEffectActor(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; UFUNCTION(BlueprintCallable) void ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass); UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") TSubclassOf<UGameplayEffect> InstantGameplayEffectClass; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") TSubclassOf<UGameplayEffect> DurationGameplayEffectClass; 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上查找并返回UAbilitySystemComponent UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor); if(TargetASC==nullptr) return; check(GameplayEffectClass); //创建了一个游戏效果的内容句柄,提供游戏效果的信息 FGameplayEffectContextHandle TargetASCContext =TargetASC->MakeEffectContext(); //把自己作为游戏效果的源对象,确保AbilitySystem可以识别到自己 TargetASCContext.AddSourceObject(this); //这里创建了一个游戏效果规范,GameplayEffectClass 是要应用的游戏效果的类,1.0f 是游戏效果的初始生命周期倍率,TargetASCContext 是游戏效果的上下文. 调用 MakeOutgoingSpec 函数,将会根据提供的参数创建一个游戏效果规范 FGameplayEffectSpecHandle EffectSpecHandle = TargetASC->MakeOutgoingSpec(GameplayEffectClass,1.0f,TargetASCContext); TargetASC->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get()); }

回到蓝图内,新建Gameplay Effect类Actor,并且命名GE_HealthPotions

修改Gameplay Effect数据

在BP_HealthPotions内修改之前的蓝图

魔法值的Gameplay Effect也同理

最后是增加最大生命值,需要把Duration Policy修改为Has Duration

蓝图使用Duration Gameplay Effect