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

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.


2
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…

ok… interesting funfact… since… it´s still not working… or… working but not really… damnit…

I updated the Definition and added a GEngine print to Screen, with the current Cost, the regeneration of the Attribute and the Outcome after calculation…

I duplicated the Class 3 Times, one for each Cost attribute in order: Stamina, health, mana, Arcanum

I also set the Costs (in the the order mentioned):
25, 10, 20, 30

Problem 1:
The AttributeCost print… is always 0… no matter what costs are set… BUT… and that´s the interesting part… the Attribute gets reduced BY that cost… Except for Stamina… stamina is not working… Even its the same functionality and SetByCaller as the other Attributes…

Here is how i set all:

And here is what it looks in Game:
2
(Arcanum start at 0… but its working on a previous test…)

Questions now:
Why are the Prints of GEngine all 0… But the value gets subtracted from the Attributes?
Why is stamina still not doing anything… Even it´s the same calculation as the other Atts?

Works now…
found out about an abandoned Tag that block stamina drain…
so… i completely reworked.my Tag System and now its running without a problem.

I still don’t know why thw GEngine Prints are showing 0 as Cost and Out value… but … since it reduces the attributes cirrectly… who cares :person_shrugging:t2:

If you need a way to calculate the costs of your abilities… just copy my code of the OP and change it to your needs… to have a point of start…

2 Likes