I have an Int variable in my character called PlayerHealth and I was wondering if there was a way to stop it from going negative and to set a max value. Thanks
There’s a few ways to do this…
Within the editor, you can set min and max values.
Alternatively, you can use the Clamp node. Anytime something wants to change the health value, run it through the clamp node
How can I do this within the editor?
Of course, this only works if there are absolute values. If you need flexible limits (for example if the maximum health grows with the character’s level), then you need to clamp the value yourself whenever the health changes.
Sorry I’m still kind of new to UE4 but when and how do I use the clamp node?
Plug your integer value into the Value input, then set the minimum and maximum values you want to limit your output value at. Here you can see an example of how it can be used in the material editor. The logic is the same; Math Expressions | Unreal Engine Documentation
Sadly, if you play around the maximum values for an int… It will not prevent the overflow to occur.
So if per non-luck you add let’s say even 30 and you miss the exact number, then you will overflow…
This for example will not be able to clamp you to the max value, because you will have already overflown by the time you do the addition.
I’m also noting that the value range is being completely ignored.
You have to do a check and control it when close to the minimum or maximum ranges.
First example let’s consider maximum value. You want a maximum amount of ammo to be 99. Let’s also say for this example that you can gain ammo in increments of 1 or 5 only. You must clamp your int maximum value at 98 NOT 99. 98 + 1 = 99 and the minimum ammo you can pick up is going to be 1. But what happens if you have 98 ammo and you pick up 5? Yes you will go over the maximum value and end up with 103 ammo. To control this you must use a branch. First check is the value <= 94? if true then +5 like normal. If false then set ammo 99 explicitly. Once you set the value to 99 you are in the clamp zone and no more ammo can be picked up. Because you already clamped the value to 98 you won’t need to do this for picking up single ammo as mentioned before 98 + 1 = 99 the target maximum value.
What about ammo depletion? What if there is an instance where an amount of ammo higher than 1 can be consumed? Let’s run this idea in reverse. Say you can consume 1 or 5 ammo at a time. Clamp the int minimum value at 1 because like before 1 - 1 = 0 but 1 - 5 = -4 so we will end up with 4 below the target minimum value. To avoid this add a check again when consuming ammo in multiples of 5. Treat any value <5 as 0 ammo by again asking is the value => 5 and if true allow the ammo to be consumed. If false do nothing simple as that. When using single ammo it will consume ammo to 0.
The attached image of my blueprint shows how to control the +5 increase. This example is for castlevania style hearts where a player can pick up 1 or 5 at a time. In the example I am using an event so that when G is pressed it acts like the player has gained 5 hearts.
What?? No! No, no, no no. This is way too complicated.
Just add the amount of ammo you picked up to your current total, then set your ammo count to the clamped value.
Ammo += PickupAmount;
Ammo = FMath::Clamp(Ammo, 0,99);
Done. Super simple. Doesn’t need to be any more complicated.
THIS is the answer^ @Slayemin
been stuck on this for a while now for C++