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放进去

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

如此一来算是成功了吗

相关推荐
日月云棠21 小时前
UE5 Lyra Weapons 模块深度分析——从装备实例到弹道散布的完整武器系统
ue5
清泓y1 天前
UE5程序化生成技术
ue5
清泓y2 天前
UE5--VR与AR开发技术
ue5·ar·vr
日月云棠2 天前
UE5 Lyra Messages 模块深度解析——从一条击杀消息看游戏事件系统的设计哲学
游戏·ue5
日月云棠3 天前
UE5 Lyra Input 模块:从手柄摇杆到技能释放,一条链路如何做到零耦合
ue5
日月云棠6 天前
UE5 Lyra源码分析——Audio_Analysis音频模块全面分析
ue5·音视频
成都渲染101云渲染66666 天前
2026云渲染平台哪个好?建筑、动画、UE5项目选择云渲染的3个关键标准
ue5
清泓y7 天前
UE 物理系统知识分享
面试·ue5·游戏程序
清泓y7 天前
UE移动开发技术面试题
android·面试·ue5·ue4·游戏程序
清泓y7 天前
UE基础知识与引擎架构面试题
面试·架构·ue5·ue4·游戏程序