RPG24.设置武器伤害(二):将效果应用于目标
1.打开XMBGameplayTags,添加新的标签用于在造成伤害时进行激活计算
ARPG_GRIVITY_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(Shared_SetByCaller_BaseDamage);
ARPG_GRIVITY_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(Player_SetByCaller_AttackType_Light);ARPG_GRIVITY_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(Player_SetByCaller_AttackType_Heavy);
UE_DEFINE_GAMEPLAY_TAG(Shared_SetByCaller_BaseDamage, "Shared.SetByCaller.BaseDamage");UE_DEFINE_GAMEPLAY_TAG(Player_SetByCaller_AttackType_Light, "Player.SetByCaller.AttackType.Light");UE_DEFINE_GAMEPLAY_TAG(Player_SetByCaller_AttackType_Heavy, "Player.SetByCaller.AttackType.Heavy");
2.进入XMBGameplayAbilityBase,创建新函数,用于制作角色的伤害句柄
//返回一个伤害效果UFUNCTION(BlueprintPure, Category = "XMB|Ability")FGameplayEffectSpecHandle MakeXMBDamageEffectSpecHandle(TSubclassOf<UGameplayEffect> EffectClass,float InWeaponBaseDamage,FGameplayTag InCurrentAttackTypeTag, int32 InCurrentCombatCount);
FGameplayEffectSpecHandle UXMBGameplayAbilityBase::MakeXMBDamageEffectSpecHandle(TSubclassOf<UGameplayEffect> EffectClass, float InWeaponBaseDamage, FGameplayTag InCurrentAttackTypeTag,int32 InCurrentCombatCount)
{//检查EffectClasscheck(EffectClass);//创建HandleFGameplayEffectContextHandle ContextHandle = GetXMBAbilitySystemComponentFromActorInfo()->MakeEffectContext();ContextHandle.SetAbility(this);//设置Ability实例ContextHandle.AddSourceObject(GetAvatarActorFromActorInfo());//设置源对象ContextHandle.AddInstigator(GetAvatarActorFromActorInfo(),GetAvatarActorFromActorInfo());//在这一步指定ability的使用者和abiility的拥有者FGameplayEffectSpecHandle EffectSpecHandle = GetXMBAbilitySystemComponentFromActorInfo()->MakeOutgoingSpec(EffectClass,GetAbilityLevel(),ContextHandle);//设置伤害EffectSpecHandle.Data->SetSetByCallerMagnitude(XMBGameplayTags::Shared_SetByCaller_BaseDamage,InWeaponBaseDamage);//设置当前攻击类型if (InCurrentAttackTypeTag.IsValid()){EffectSpecHandle.Data->SetSetByCallerMagnitude(InCurrentAttackTypeTag, InCurrentCombatCount); }return EffectSpecHandle;
}
3.新建一个计算类
4.打开XMBStructTypes.h,向FXMBWeaponBase内添加用于记录武器基础伤害的数据表格
USTRUCT(BlueprintType)
struct FXMBWeaponData
{GENERATED_BODY()UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)TSubclassOf<UPlayerLinkedAnimLayer> WeaponAnimLayerToLink;UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)UInputMappingContext* WeaponInputMappingContext;UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, meta = (TitleProperty = "InpuTTag"))TArray<FXMBAbilitySet> DefaultWeaponAbilities;//数据表格UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)FScalableFloat WeaponBaseDamage;
};
5.打开PawnCombatComponent.h,向内添加两个函数
//获取当前装备的武器UFUNCTION(BlueprintCallable, Category = "Warripr|Combat")APlayerWeapon* GetPlayerCurrentEquippedWeapon() const;//获取当前武器的伤害UFUNCTION(BlueprintCallable, Category = "Warripr|Combat")float GetPlayerCurrentEquipWeaponDamageAtLevel(float InLevel) const;
APlayerWeapon* UPlayerCombatComponent::GetPlayerCurrentEquippedWeapon() const
{return Cast<APlayerWeapon>(GetCharacterCurrentEquippedWeapon());
}float UPlayerCombatComponent::GetPlayerCurrentEquipWeaponDamageAtLevel(float InLevel) const
{return GetPlayerCurrentEquippedWeapon()->PlayerWeaponData.WeaponBaseDamage.GetValueAtLevel(InLevel);
}
6。启动项目,创建一个曲线表格用于记录武器伤害
打开武器,搜索basedamage,选择表格
7.启动项目,创建共享ge
然后选择计算伤害
8.打开LightAttackmaster
9。现在要将效果应用于目标
打开XMBGameplayAbility.h
FActiveGameplayEffectHandle NativeApplyEffectSpecHandleToTarget(AActor* TargetActor, const FGameplayEffectSpecHandle& InSpecHandle);
FActiveGameplayEffectHandle UXMBGameplayAbility::NativeApplyEffectSpecHandleToTarget(AActor* TargetActor,const FGameplayEffectSpecHandle& InSpecHandle)
{UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);return XMBGetAbilitySystemComponentFromActorInfo()->ApplyGameplayEffectSpecToTarget(*InSpecHandle.Data,TargetASC);
}
再创建一个函数用在蓝图里使用,为了确保是否将效果应用给目标,创建一个函数进行验证,和一个枚举用于返回是否应用于目标的结果
先创建枚举
UENUM()
enum class EWarriorSuccessType : uint8
{Successful,Failed
};
然后创建函数
UFUNCTION(BlueprintCallable, Category = "Warrior|Ability",meta = (displayname = "Apply Gameplay Effect Spec Handle To Target Actor",ExpandEnumAsExecs = "OutSuccessType"))FActiveGameplayEffectHandle BP_ApplyEffectSpecHandleToTarget(AActor* TargetActor, const FGameplayEffectSpecHandle& InSpecHandle,EWarriorSuccessType& OutSuccessType);
FActiveGameplayEffectHandle UXMBGameplayAbility::BP_ApplyEffectSpecHandleToTarget(AActor* TargetActor,const FGameplayEffectSpecHandle& InSpecHandle, EWarriorSuccessType& OutSuccessType)
{FActiveGameplayEffectHandle ActiveGameplayEffectHandle = NativeApplyEffectSpecHandleToTarget(TargetActor,InSpecHandle);OutSuccessType = ActiveGameplayEffectHandle.WasSuccessfullyApplied() ? EWarriorSuccessType::Successful : EWarriorSuccessType::Failed;return ActiveGameplayEffectHandle;
}
10.启动项目,打开LightAttackMaster