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

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

如此一来算是成功了吗

相关推荐
zhangzhangkeji6 天前
UE5 C++(71):文件是否存在,文件夹是否存在,FPaths :: FileExists( const FString & InPath) ;
ue5
妙为6 天前
UE5角色穿过石头穿模
ue5·unreal engine5·角色穿越石头·穿模
技术策划Boring7 天前
2025年工作复盘:开放世界3A项目配置管线与性能监控的探索
游戏·ue5·虚幻·p4·perforce
zhangzhangkeji9 天前
UE5 C++(70-2):定义成员函数 getCleanDirectory(..) 和枚举类 EFileDirectoryType,来获得目录
ue5
avi911110 天前
UE4-UE5虚幻引擎-前置学习三,优化,基础CPP
ue5·ue4·游戏开发·虚幻·游戏优化·游戏代码
zhangzhangkeji10 天前
UE5线程进阶(3-2):任务图的相关源码整理。 FGraphEvent 与 TGraphTask 的区别和联系
ue5
zhangzhangkeji12 天前
UE5线程进阶(3-1):
ue5
zhangzhangkeji12 天前
UE5线程进阶(2-3):enum ENamedThreads命名空间 :: Type : int32 { RHIThread = 0 } 是渲染硬件接口线程
ue5
zhangzhangkeji13 天前
UE5线程进阶(2-1):枚举类EAsyncExecution,作业类TAsyncRunnable、TAsyncQueuedWork,及全局线程函数 Async(..),及线程调用的 4 种方法总结
ue5
zhangzhangkeji14 天前
UE5线程进阶(1):
ue5