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 oneGet 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:
- ensure it’s filled to the brim with good health; even though the progress bar reads
Percent
, it is actually a 0-1 range:
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…
…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.