How to use GameEffect [in GameplayAbilitySystem] to make a Temporary effect

I made a character whose maximum HP is 100. Now I want to use gas to make a buff. The effect is like this: it can make the character recover 50 HP in 2 seconds, and the amount of HP after recovery can’t be more than 100. After that, it will return to its original appearance.
So I made a GameEffect, using the has duration type, with scalable float magnet set to 2, and add character’s HP attribute 50. Then there is a problem, because GameEffect of has duration type will not call the function UAttributeset:: PostGameplayEffectexEcution after execution (I usually limit the value range of HP here), so I can’t limit the range of returned HP, and the amount of HP after recovery will over 100.

What‘s the right way to make the buff?

If I’m understanding the question correctly you want to clamp the HP so it doesn’t go over 100 at any point?

Locally, in my project, I use PostGameplayEffectExecute() to clamp the Health attribute:

else if (Data.EvaluatedData.Attribute == GetHealthAttribute())
{
	SetHealth(FMath::Clamp(GetHealth(), 0.0f, GetMaxHealth()));
}

I understand that you say that this doesn’t make a difference, but perhaps you also need to add clamping to the PreAttributeChange() function as well inside your attributes. For my project, I have this:

if (Attribute == GetHealthAttribute())
{
	NewValue = FMath::Clamp<float>(NewValue, 0.0f, GetMaxHealthAttribute().GetNumericValueChecked(this));
}

As a test, I created a gameplay effect with a duration of 100 seconds that adds 50 to my Health attribute:

In-game, my Health does not exceed my current Max Health. As an additional test, to at least ensure this gameplay effect was doing something, I set the modifier magnitude to -1 instead of 50; subtracting one from my Health every second. Making this change showed my character losing health per second, so its safe to say that when using a value of 50, or anything positive, meant it was attempting to add to my Health. But due to my two different clamps, it would not go past my Max Health.

I hope this helps in some way. Good luck.

2 Likes