UE5多人MOBA+GAS 番外篇:将冷却缩减属性应用到技能冷却中

在技能基类CGameplayAbility中添加浮点数

cpp 复制代码
	// 技能冷却
	UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Cooldown")
	FScalableFloat CooldownDuration;

技能的冷却时长就不放在GE里了,放在技能的里面去设置

创建一个MMC我就废物利用把之前MMC伤害的直接拿来用了

cpp 复制代码
#pragma once

#include "CoreMinimal.h"
#include "GameplayModMagnitudeCalculation.h"
#include "MMC_BaseAttackDamage.generated.h"

/**
 * 
 */
UCLASS()
class UMMC_BaseAttackDamage : public UGameplayModMagnitudeCalculation
{
	GENERATED_BODY()
	
public:
	UMMC_BaseAttackDamage();
	virtual float CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec& Spec) const override;
private:
	FGameplayEffectAttributeCaptureDefinition CooldownReductionCaptureDef;
};
cpp 复制代码
#include "GAS/MMC/MMC_BaseAttackDamage.h"

#include "GAS/Core/CAttributeSet.h"
#include "GAS/Core/CGameplayAbility.h"
#include "GAS/Core/CHeroAttributeSet.h"

UMMC_BaseAttackDamage::UMMC_BaseAttackDamage()
{
	// 获取冷却缩减属性
	CooldownReductionCaptureDef.AttributeToCapture = UCHeroAttributeSet::GetCooldownReductionAttribute();
	CooldownReductionCaptureDef.AttributeSource = EGameplayEffectAttributeCaptureSource::Source;

	// 添加捕获属性
	RelevantAttributesToCapture.Add(CooldownReductionCaptureDef);
}

float UMMC_BaseAttackDamage::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec& Spec) const
{
	FAggregatorEvaluateParameters EvalParams;
	// 绑定源/目标标签
	EvalParams.SourceTags = Spec.CapturedSourceTags.GetAggregatedTags();
	EvalParams.TargetTags = Spec.CapturedTargetTags.GetAggregatedTags();
	// 获取Ability实例
	const UCGameplayAbility* Ability = Cast<UCGameplayAbility>(Spec.GetContext().GetAbilityInstance_NotReplicated());
	if (!Ability) return 0.0f;
	
	// 获取基础冷却时间
	float BaseCooldown = Ability->CooldownDuration.GetValueAtLevel(Ability->GetAbilityLevel());

	// 获取冷却缩减属性值
	float CooldownReduction = 0.f;
	//(通过CooldownReductionCaptureDef定义的捕获规则)
	GetCapturedAttributeMagnitude(CooldownReductionCaptureDef, Spec, EvalParams, CooldownReduction);	// 获取源的属性值

	// 计算冷却
	float ActualCooldown = BaseCooldown * (1.0f - CooldownReduction/100.0f);

	return FMath::Max(0.1f, ActualCooldown);
}

修改一下函数库CAbilitySystemStatics中获取技能冷却的函数GetCooldownDurationFor

cpp 复制代码
float UCAbilitySystemStatics::GetCooldownDurationFor(const UGameplayAbility* AbilityCDO,
	const UAbilitySystemComponent& ASC, int AbilityLevel)
{
	float CooldownDuration = 0.f;
	if (AbilityCDO)
	{
		const UCGameplayAbility* Ability = Cast<UCGameplayAbility>(AbilityCDO);
		if (!Ability) return CooldownDuration;
		
		// 获取基础冷却时间
		float BaseCooldown = Ability->CooldownDuration.GetValueAtLevel(AbilityLevel);

		// 获取冷却缩减属性值
		bool bFound;
		float CooldownReduction = ASC.GetGameplayAttributeValue(UCHeroAttributeSet::GetCooldownReductionAttribute(), bFound);
		if (bFound)
		{
			// 计算最终冷却时间
			CooldownDuration = BaseCooldown * (1.0f - CooldownReduction/100.0f);
		}
	}

	// 返回绝对值(确保冷却时间始终为正数)
	return FMath::Abs(CooldownDuration);
}

冷却的GE的持续时间然后调用计算类把做好的mmc放进去

初始化的时候修改一下这里

如此一来算是成功了吗

相关推荐
weixin_404679312 天前
虚幻5电子书
ue5
directx3d_beginner3 天前
5,动画蓝图类接口转c++
ue5
RuiZN3 天前
UE5 蓝图 FPS 02 Event Beginplay
c++·ue5
RuiZN3 天前
UE5 蓝图 FPS 01 Event Tick
c++·ue5
directx3d_beginner3 天前
6,执行攻击改为c++
ue5
平行云3 天前
实时云渲染预启动技术解析:UE数字孪生应用的延迟优化机制(二)
linux·unity·ue5·webgl·实时云渲染·云桌面·像素流
RuiZN4 天前
UE5 UObject类详解
c++·ue5
平行云4 天前
实时云渲染预启动技术解析:UE数字孪生应用的延迟优化机制(一)
linux·ue5·webgl·数字孪生·云渲染·实时云渲染·像素流
RuiZN4 天前
UE5 UObject和反射
c++·ue5
远离UE46 天前
UE5 如何在延时渲染在材质编辑器提前使用到多灯光的数据
ue5·编辑器·材质