Widget Healthbar binding to actor

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”

public:	

	UPROPERTY(VisibleAnywhere, BluePrintReadOnly)
		float Health;

	UPROPERTY(VisibleAnywhere, BluePrintReadOnly)
		float MaxHealth = 120;

now in the Widget blueprint I tried something like this…


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

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

  • 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


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:

  • 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:

  • 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

…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.

3 Likes

Works like a charm.
Thank you. :wink:

1 Like