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

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

如此一来算是成功了吗

相关推荐
一个响当当的名号10 小时前
lectrue5 存储模型和压缩
ue5
zhangzhangkeji13 小时前
UE5 C++(44-4):对比一下蓝图中的射线检测节点,源代码,按通道与按对象类型
ue5
暮志未晚Webgl1 天前
UE使用内置功能查看性能
ue5
AI视觉网奇1 天前
Epic linux 打包。
笔记·学习·ue5
伪善者1 天前
UE5 打包插件
ue5·打包
AI视觉网奇1 天前
ue5 开发 web socket server 实战2026
c++·学习·ue5
zhangzhangkeji2 天前
UE5 C++(39):创建 TimeHandle 定时器
ue5
zhangzhangkeji2 天前
UE5 C++(38):创建 Interface接口
ue5
zhangzhangkeji2 天前
UE5 C++(40):创建 3DWidget 并渲染到屏幕上,涉及类 UUserWidget 与 UWidgetCompopent
ue5
zhangzhangkeji2 天前
UE5 C++(41):创建 ApplyDamage 并接受伤害 TakeDamage
ue5