# Widget Healthbar binding to actor

**URL:** <https://forums.unrealengine.com/t/widget-healthbar-binding-to-actor/501986>\
**Category:** Blueprint\
**Tags:** unreal-engine, UE4-27, Blueprint, question\
**Created:** [February 25, 2022, 5:04am UTC](https://forums.unrealengine.com/t/widget-healthbar-binding-to-actor/501986 "2022-02-25T05:04:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrus\_80](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cyrus\_80](https://forums.unrealengine.com/u/Cyrus_80)\
**Post date:** [February 25, 2022, 5:04am UTC](https://forums.unrealengine.com/t/widget-healthbar-binding-to-actor/501986/1 "2022-02-25T05:04:13Z")

</div>

I am actualy a programmer, but it was advised to use blueprints when binding a actors health with the actors healthbar.

Now the widget is placed above the Enemys head (actor) and it has a maxhealth of 100, every time i damage it it does -25 after 4 hits it destroys that Enemy. this is working perfeclty

Now im trying to bind the healhbar widget percentig, and i have no qlue what im doing  
I have watched some tutorials on youtube, some of them completely creating the game in Blueprints wich is not what im doing.

My actor “Enemy.h”

```auto
public:	

	UPROPERTY(VisibleAnywhere, BluePrintReadOnly)
		float Health;

	UPROPERTY(VisibleAnywhere, BluePrintReadOnly)
		float MaxHealth = 120;

```

now in the Widget blueprint I tried something like this…

 ![Blueprint1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/a/f/eafcd3516d1c3ccc1cf41afa215f18c9963d157e.png)  
 ![Blueprint2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/1/5/615ca8462c1561df5ee7549bce2dfc0b468fc626.png)

The Health UPROPERTY from the Enemy.h keeps the health up to date  
so i dont think thats the problem. it must be something with the blueprints above.

Please help me out because at this point im just guessing.  
Maybe some blueprints picture examples ,if that would be possible would be great

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 25, 2022, 9:59am UTC](https://forums.unrealengine.com/t/widget-healthbar-binding-to-actor/501986/2 "2022-02-25T09:59:24Z")

</div>

> [@Cyrus\_80](#):
>
> it was advised to use blueprints when binding a actors health with the actors healthbar.

Bound functions execute every frame, this variable would be polled every frame. Not a biggie if it’s just a bunch of entities, but if _everyone_ is doing it all the time, it may become a performance factor; besides, not a good practice to start with - why waste cycles. Blueprints are sluggish enough. Consider adopting an Event Driven approach instead, whenever you can.

* * *

Regarding why what you tired does not work:

- the `New Health` variable is not updated when you take damage. It’s updated only once - when the widget is constructed, added to the screen. No further updates are done.
- the `Get All Actors of Class` node has no class selected in the dropdown. It also does what is says on the tin - it gets _ALL_ instance of this class, and we don’t even know the order so you don’t know which one `Get 0` fetches! Besides that, no point in getting ALL instances - we want to update only this one, the one belonging to the enemy getting hurt.

* * *

> Please help me out because at this point im just guessing.  
> Maybe some blueprints picture examples ,if that would be possible would be great

## The Widget

- create a widget with just a progress bar, no canvas; no script goes in here:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/8/6/486af08343dc113073c3704ed0200c0e70a2c46d.png)

- ensure it’s filled to the brim with good health; even though the progress bar reads `Percent`, it is actually a 0-1 range:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/4/b/74b64389ad109c62a8206b26ab5172700c25b82f.png)

* * *

## The Enemy

- make a new actor class with a couple of variables representing health, ensure their default values make sense - here it’s 100/100. You can do this in C++ or BPs:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/f/c/9fcdba7bb78755035c21493753229a6d4e3b3c41.jpeg)

- I added a `Static Mesh` component to the actor so we can see it represented with this purple pyramid in the world
- add a `Widget Component` - it will hold onto, display and maintain the lifecycle of the progress bar widget we created earlier
- ensure you’re happy with the `Draw Size` and Z offset - so the bar appear above the actor’s mesh (you can also use Screen Space instead of World Space)

* * *

## Updating the widget

- when the game begins The Enemy actor queries its own widget component and creates a direct reference to the widget the component instantiated - at this point it’s more convenient than casting every time we want to access it:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/1/b/11bbb8f625ca17c931ad5e6305c91ab19181bd24.png)

- above, when this actor is dealt damage (via a snazzy built-in system), we calculate the damage done and update the progress bar using the abovementioned reference
- here it is set up in such a way that we can’t receive damage more often than twice a second

* * *

## Dealing Damage

- when other world entities interact with The Enemy actor, they now can…

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/d/4/0d47b38785e01a356cd8fac3efaefdcb713c00e9.png)

…resulting in:

* * *

The above can be set up in many ways but I did not want to involve Event Dispatchers here.  
Hope it makes sense.

---

<div class="post-metadata">

**Author:** ![d4brain](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/d4brain/32/727542_2.png) [@d4brain](https://forums.unrealengine.com/u/d4brain)\
**Post date:** [August 6, 2023, 8:47am UTC](https://forums.unrealengine.com/t/widget-healthbar-binding-to-actor/501986/3 "2023-08-06T08:47:43Z")

</div>

Works like a charm.  
Thank you. 😉
