How is a "Modifier Magnitude Calculation" working for Ability Costs?

BDC_Patrick
Thu, 13 Apr 2023 11:06:26 +0000
Blueprint

So… hi there…
I finally got my Base Ability and the whole Ability DataTable with all of it´s stats and values to work…
With just a small… nearly insignificant problem… The Costs of Abilities…

For now… if I want to have 80+ Abilities later… I currently would create 80+ GameEffects just for the costs… And that would be… ridiculous… and confusing…

So… i read about the UGameplayModMagnitudeCalculation and created a small Class for that.
This one…

Header:

#pragma once

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

UCLASS()
class LOH_THETAVERN_API UMMC_Calc_StaminaCost : public UGameplayModMagnitudeCalculation
{
	GENERATED_BODY()

private:
	FGameplayEffectAttributeCaptureDefinition StaminaRegDef;

public:
	UMMC_Calc_StaminaCost();
	virtual float CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec& Spec) const override;
};

Definition:

#include "GAS/Execs/MMC_Calc_StaminaCost.h"
#include "GAS/TheTavern_AttributeSet_Base.h"
#include "EngineUtils.h"


UMMC_Calc_StaminaCost::UMMC_Calc_StaminaCost()
{
	StaminaRegDef.AttributeToCapture = UTheTavern_AttributeSet::GetStaminaRegAttribute();
	StaminaRegDef.bSnapshot = false;
	RelevantAttributesToCapture.Add(StaminaRegDef);
}

float UMMC_Calc_StaminaCost::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec& Spec) const
{
	const FGameplayTagContainer* SourceTags = Spec.CapturedSourceTags.GetAggregatedTags();

	FAggregatorEvaluateParameters EvaParas;
	EvaParas.SourceTags = SourceTags;

	// Calculation Variables
	float Regeneration = 0.0f;
	GetCapturedAttributeMagnitude(StaminaRegDef, Spec, EvaParas, Regeneration);
	Regeneration = FMath::Clamp(Regeneration, 0.0f, 100.0f);

	const float Costs = Spec.GetSetByCallerMagnitude(FGameplayTag::RequestGameplayTag(FName("Exec.Calc.StaminaCost")), true, 0.0f);

	// Make Factor
	Regeneration /= 100.0f;

	// Return Magnitude
	const float Out = Costs - (Costs * Regeneration);

	if(GEngine)
	{
		GEngine->AddOnScreenDebugMessage(0, 8, FColor::Red, "StaminaCost:       " + FString::SanitizeFloat(Costs, 0));
		GEngine->AddOnScreenDebugMessage(1, 8, FColor::Red, "StaminaReg Factor: " + FString::SanitizeFloat(Regeneration, 2));
		GEngine->AddOnScreenDebugMessage(2, 8, FColor::Red, "Out:               " + FString::SanitizeFloat(-Out, 0));
	}

	return Out > 0.f?-Out:0.0f;
}

My Question now…
How is this supposed to work…?

I have the Modifier set in the GCost_Execution GameplayEffect, that is applied to the Costs Slot of the Base GameplayAbility.

I Set the GameplayTag for the StaminaCost to a new magnitude in the Ability itself:

(The value of this Struct is Set in the DataTable)

But the Stamina never gets reduced…

3 posts - 1 participant