UMG what event can update a healthbar?

I create an UMG widget progressbar everytime i get damage but i think this is not what i want since it wont update if i heal myself. 2 Questions:
1.Is there an event that triggers if the health variable changes?
2.Do i have to “create widget” every time? Does it mean he just updates the old or does he really create new widget on the top of the previous on? This would crash the performance by time.

I don’t know if this could work, because i justed started with UMG, but i guess you can the following:

First, i would create a reference to the Widget in my CharacterBlueprint.
In the Eventgraph of the Widget, you can call an event called “On Construct”. There i would get the playercharacter, cast it and set the Reference to the Widget (self).

Now i would create a Float Variable called “PercentLife” in the WidgetBlueprint.
Than go to the ProgressBar and on the right search for “Percent”. There you click on Bind and choose the “PercentLife” variable.

And now you go to your Character Blueprint and get the Reference you created earlier. Get the “PercentLife” Variable of it and set it to the following:

(CurrentHealth/MaxHealth)

That should work, at least in my head :X

Sorry, made a small mistake. Percent is from 0.0 to 1.0, so dont multiply with 100. Corrected that.

Hm, you could start a Timer in the Widget and let it loop every 0.1 Sec to set the Percentage of the bar equal to the Percentage of your character. Thats like an Event Tick.

Hm, it doesnt work that way. The thing is i need something that updates the healthbar everytime the health changes. In the CharacterBP there is a function Set Percent so i can basicly just put the health variable out of mycharacter to the widget but the problem is how do i update it if the variable changes?

in that case i could also do an event tick haha :stuck_out_tongue: but i think it drains to much performance. i read once there is something like “event if value changes” but i dont rly know where i read this

Hm, what if you save a reference of your Widget somewhere, where you can access it and create a function in your Widget that updates the Percentage of the Bar with a Float as input. Than you could call the Function with your damage/heal function and just plugin the percentage from your player. :confused: Sorry if i can’t come up with a real solution. Hope that still helps a bit.

Hello,

I spent some time working on this, but I found the best option was to Cast To the widget when I wanted to make an update. I was making a Flashlight the drains over time already and wanted to visualize that with a progress bar. Here is the technique I used to update the bar:

Make a UserWidget Variable (In my example it is called “Hud Widget”) in your blueprint that will be communicating when it is hit. You only need to Create Widget once for a HUD normally; In my example I do this in MyCharacter on Event Begin Play.

After you create your widget, you can set the UserWidget variable to the return value of the Create Widget node. This will allow us to call up the widget later to set the Progress Bar.

For your Health, you can ignore the clamp and set I do in the beginning. The event I am using is custom, but here you would do it On Hit instead for your health.

After setting the UserWidget variable “Hud Widget” we can get a reference to it, then do a “Cast To (Widget name here)”. From that Cast To’s return value, you can drag and retrieve the progress bar’s variables and Set Percent.

I used up my attachments, more answer coming.

In the previous image, you can see I am setting a variable “Display Battery” that is a variable I created in the widget to pass to the progress bar

13172-widget_fl02.jpg

That is all that is within the Widget’s progress bar function. It is just passing the information along.

I should have mentioned in the beginning that I had to bind the progress bar’s “Percent”, which I named “Battery Bar”. This would be your Health Bar.

Let me know if you have any questions. I am sure there are areas I can help clarify better.

Thanks alot, i changed it, now it just creates the widget once at the begining and then just updates with the cast. What about the “add to viewport” do i have to remove the old one and add a new one every time or is he just updating the progressbar?

Do i realy have to set like in your last picture a variable to the return node? Because i tried your way without this and it worked out. The “Set percent” works as standalone without a variable.

Last question what kind of event should i use to determine that my health changed? Event tick is to much i think and your suggested On Hit wont work because i want it to update if i heal myself also.

What about the “add to viewport” do i
have to remove the old one and add a
new one every time or is he just
updating the progressbar?

No, you just have to add it the once.

Do i realy have to set like in your
last picture a variable to the return
node?

I was told the widget would need it, but if it work without it, then that’s fine as well. You can use it to reference the variable in the widget more easily now at the least. Feel free to get rid of it if you can make it work without.

Last question what kind of event
should i use to determine that my
health changed?

Personally I would create a custom event named “Update Health” that has a float input. Then anything could call that and give it an amount to change. Here’s an example:

Let me know if you have any questions about it.