Health Bar Going to Negative Numbers

Alright so in my ThirdPersonCharacter bp I have three variables, MaxHealth, CurrentHealth and HealthRegePercentage; inside a function these variables control a progress bar widget that regenerates health until full, and upon CurrentHealth being equal or greater than MaxHealth no more health is added.

The update health function is here: 2n6OpZ5Y posted by anonymous | blueprintUE | PasteBin For Unreal Engine 4

Now that works all fine and dandy, upon the variables being equal to one another, no more health is added. However, in order to test this theory I created two more variable inputs, damage and heal. When V key is pressed, inside the Event Graph of 3rdPerson BP, 5 damage is sent to the update health function to be subtracted from the health regeneration rate total that is being set to current health, the same thing in reverse is done with the heal float, 5 points are added to the regeneration total before being set.

This works until you get close to your max health, I am able to add and subtract from current health perfectly fine, however if you are at 99 health and heal yourself 5 points, the float obviously goes beyond 100 (max health value) and does not regenerate automatically.

Now according to my logic (theoretically), you would just have to send some damage to the update health function in order for damage to be subtracted…However this is not the case.
When you are at 99 health, and heal yourself 5 more points, the progress bar converts this number into its negative equivalent -104 and starts regaining health, because the variable current health is less than positive 100…

Any suggestions on how to fix this? I know its a flaw in my logic, however I can not see the error, working with blueprints is new for me, however programming is not. I just wasn’t catching onto ue4s c++ documentation and decided to go with blueprints. That and a lot of tutorials are outdated :stuck_out_tongue:

I’m not really sure what is causing that bug, but I’m assuming you don’t want the player’s health to go above max health correct? Instead of letting current health go to 104, just use a clamp float node to prevent the player’s health from going above max health(100 in your case). This change may fix your bug.

just use a clamp node from Min = 0 & Max = Your maximum hp

1 Like

Ill give that a try, thanks guys.

So Ive tried a few things, and I cannot seem to get the clamp to work correctly, how would I go about using it in my BP? Or how would one use it correctly in similar circumstances?

Just when you calculate the + or - health run it into a clamp with a min and max value and run the output from that to set your current HP. That way if it goes to 104 it will still just go to 100.

I must admit that I didn’t know “clamp” before this post, so I checked it and it will work (very useful with thinks like this, thanks for it , Bjorn and ZoltanJr).

However, let imagine that clamp doesn’t exists. In this case you would need check if the result will be correct. So before to subtract (or increase) current health, you must check the result:

[FONT=Lucida Console]If Current health + Health healed > Max Health then
Current Health = Max Health
else
Current Health = Current Health + Health healed

This is just the logic that you was missing in your code. But use a Clamp, it really make it easier, even to understand in a future.