创建MMOARPGGameplayAbilityType.h,包含两个结构体
#pragma once
#include "CoreMinimal.h"
#include "Abilities/GameplayAbilityTargetTypes.h"
#include "MMOARPGGameplayAbilityType.generated.h"
/**
*
*/
USTRUCT(BlueprintType)
struct FMMOARPGGameplayEffects
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = GameplayEffects)
TArray<TSubclassOf<UGameplayEffect>> TargetEffectClasses;//GE技能效果容器
};
USTRUCT(BlueprintType)
struct FMMOARPGGameplayEffectSpec
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = GameplayEffects)
FGameplayAbilityTargetDataHandle TargetHandleData;//目标Handle数据
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = GameplayEffects)
TArray<FGameplayEffectSpecHandle> TargetEffectSpecs;//技能效果实例Handle
};
打开MMOARPGGameplayAbility.h,创建在蓝图可配置的GE类
#include "MMOARPGGameplayAbilityType.h"
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = GameplayEffects)
TMap<FGameplayTag, FMMOARPGGameplayEffects> EffectMap;
打开MMOARPGGameplayAbility.cpp,实现伤害事件接口,注册伤害效果
void UMMOARPGGameplayAbility::OnDamageGameplayEvent(FGameplayTag InGameplayTag, FGameplayEventData Payload)
{
//通过标签找到技能效果
if (FMMOARPGGameplayEffects* InEffect = EffectMap.Find(InGameplayTag))
{
FMMOARPGGameplayEffectSpec GameplayEffectSpec;
{
//注册TargetDataHandle
FGameplayAbilityTargetData_ActorArray* NewTargetData_ActorArray = new FGameplayAbilityTargetData_ActorArray;//存放TargetData的结构体
NewTargetData_ActorArray->TargetActorArray.Add(const_cast<AActor*>(Payload.Target));//TargetActorArray是结构体内的目标容器,将EventData数据中的目标添加到容器中,记得去Const
GameplayEffectSpec.TargetHandleData.Add(NewTargetData_ActorArray);//然后将这个NewTargetData注册到TargetDataHandle里
//注册EffectSpecHandle
for (auto& Tmp : InEffect->TargetEffectClasses)
{
FGameplayEffectSpecHandle NewEffectSpecHandle;
//MakeOutgoingSpec函数,参数(TSubclassOf<UGameplayEffect>,float Level,FGameplayEffectContextHandle),返回EffectSpecHandle
NewEffectSpecHandle = GetAbilitySystemComponentFromActorInfo()->MakeOutgoingSpec(Tmp, 1, MakeEffectContext(CurrentSpecHandle, CurrentActorInfo));
if (NewEffectSpecHandle.IsValid())
{
//通过CurrentSpecHandle寻找技能对象AbilitySpec
FGameplayAbilitySpec* AbilitySpec = GetAbilitySystemComponentFromActorInfo()->FindAbilitySpecFromHandle(CurrentSpecHandle);
//应用技能标签至技能效果对象,
ApplyAbilityTagsToGameplayEffectSpec(*NewEffectSpecHandle.Data.Get(), AbilitySpec);
if (AbilitySpec)
{
//对标签长度的量级进行赋值
NewEffectSpecHandle.Data->SetByCallerTagMagnitudes = AbilitySpec->SetByCallerTagMagnitudes;
//将EffectSpecHandle添加到容器TargetEffectSpecs中,完成注册
GameplayEffectSpec.TargetEffectSpecs.Add(NewEffectSpecHandle);
}
}
}
}
for (auto& Tmp : GameplayEffectSpec.TargetEffectSpecs)
{
//执行技能效果到目标,返回技能效果Handle
TArray<FActiveGameplayEffectHandle>ActiveGameplayEffectHandles = K2_ApplyGameplayEffectSpecToTarget(Tmp, GameplayEffectSpec.TargetHandleData);
}
}
}
|