# Gameplay attributes are not clamped and have higher values internally.

**URL:** <https://forums.unrealengine.com/t/gameplay-attributes-are-not-clamped-and-have-higher-values-internally/2153274>\
**Category:** C++\
**Tags:** unreal-engine, question\
**Created:** [November 26, 2024, 9:25pm UTC](https://forums.unrealengine.com/t/gameplay-attributes-are-not-clamped-and-have-higher-values-internally/2153274 "2024-11-26T21:25:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Youshisu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Youshisu](https://forums.unrealengine.com/u/Youshisu)\
**Post date:** [November 26, 2024, 9:25pm UTC](https://forums.unrealengine.com/t/gameplay-attributes-are-not-clamped-and-have-higher-values-internally/2153274/1 "2024-11-26T21:25:59Z")

</div>

When I refill mana by 100 points(with gameplay effect) . On my Hud I still see 100, but internally character has more and it keeps 100 until falls below 100 so I can actually see changes.

Where do I clamp values properly? I tried reading Tranek Git Documentaiton but is so confusing as it points to this methods…

I tried 2 methods of clamping my Attributes.

```cpp
#include "BasicAttributeSet.h"

void UBasicAttributeSet::PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue)
{
	if (Attribute == GetHealthAttribute()) {
		NewValue = FMath::Clamp<float>(NewValue, 0.f, MaxHealth.GetCurrentValue());
	}

	if (Attribute == GetManaAttribute()) {
		NewValue = FMath::Clamp<float>(NewValue, 0.f, MaxMana.GetCurrentValue());
		//NewValue = FMath::Clamp<float>(NewValue, 0.f, 60.f);
	}
}

void UBasicAttributeSet::PostAttributeChange(const FGameplayAttribute& Attribute, float OldValue, float NewValue)
{
	if (Attribute == GetHealthAttribute()) {
		NewValue = FMath::Clamp<float>(NewValue, 0.f, MaxHealth.GetCurrentValue());
	}

	if (Attribute == GetManaAttribute()) {
		NewValue = FMath::Clamp<float>(NewValue, 0.f, MaxMana.GetCurrentValue());
	}
}

```

---

<div class="post-metadata">

**Author:** ![JAuzanneau](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jauzanneau/32/978097_2.png) [@JAuzanneau](https://forums.unrealengine.com/u/JAuzanneau)\
**Post date:** [December 4, 2024, 2:23pm UTC](https://forums.unrealengine.com/t/gameplay-attributes-are-not-clamped-and-have-higher-values-internally/2153274/2 "2024-12-04T14:23:34Z")

</div>

In the AttributeSet header, there is this comment on the PreAttributeChange function:

`This function is meant to enforce things like "Health = Clamp(Health, 0, MaxHealth)"`

I’m pretty sure this means you need to also clamp your actual value and not just the new value. I’m not familiar with this plugin and this class, so I can’t really give you an exact line of code.

Edit:  
I also read a bit of tranek’s documentation. I found this note about AttributeSets:

**Note:** Any clamping that happens here does not permanently change the modifier on the `ASC` . It only changes the value returned from querying the modifier. This means anything that recalculates the `CurrentValue` from all of the modifiers like [`GameplayEffectExecutionCalculations`](https://github.com/tranek/GASDocumentation?tab=readme-ov-file#concepts-ge-ec) and [`ModifierMagnitudeCalculations`](https://github.com/tranek/GASDocumentation?tab=readme-ov-file#concepts-ge-mmc) need to implement clamping again.

Maybe experiment with this as well.

---

<div class="post-metadata">

**Author:** ![Kollektorr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kollektorr/32/748778_2.png) [@Kollektorr](https://forums.unrealengine.com/u/Kollektorr)\
**Post date:** [December 4, 2024, 8:50pm UTC](https://forums.unrealengine.com/t/gameplay-attributes-are-not-clamped-and-have-higher-values-internally/2153274/3 "2024-12-04T20:50:59Z")

</div>

Hi,

You can use PostGameplayEffectExecute function.

For example:

```auto
void UAuraAttributeSet::PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data)
{
if (Data.EvaluatedData.Attribute == GetHealthAttribute())
	{
		SetHealth(FMath::Clamp(GetHealth(), 0.f, GetMaxHealth()));
	}
	if (Data.EvaluatedData.Attribute == GetManaAttribute())
	{
		SetMana(FMath::Clamp(GetMana(), 0.f, GetMaxMana()));
	}
}

```

Note: you may want to keep PreAttributeChange or PostAttributeChange, no harm.
