So I was working on a UI healthbar for a certain enemy. Anybody here know hoe to properly cast/get actors of a class to set health bars to? If I have 2 enemies in my game, if I damage the first one, the healthbar on both stay the same. However, if I damage the second one, both healthbars change. But the health IS working properly, so both do die at the correct times.
So how would I properly cast to individual instances of the enemy? Thank you so much!
The attached image is how I’m grabbing the health of each one so far.
you are using the get all actors, to get all actors,(in a undefined order that can be not always the same)
so you are setting the health (lets say you have 4 actors) 4 times! and every time you get the value and set the var,
the value get overwritten 4 times and only the last remain.
so every widget you are using with this code, will load the last actor-health.
the widget should know his exact actor from the begin
To do so , create a variable "actor reference " in the widget, and name it Target
When you create a widget, pass to the widget the actor you want he to show health.
Then inside the code, you will not need anymore the get all actors (the loop neither ), but only your variable, actor, to get the healt .
here i have dropped some tank in the scene,
they have some variables editable, so i set for each tank , life and a “integer for place it in the widget”
every tank create his own widget, and set himself as the tank of that widget.
Every widget now will read the life of his own tank, and display it with a progress bar, (i binded the progress bar to the float)
Beware that i am setting the reference of the tank , as soon as it get created,
otherwise the widget will find the reference empty and start shouting errors
Thank you! This helped a lot. One last question though(sorry for asking so much, you’ve been really helpful) how would I make the health bar above the enemies? I had that part working at first, because at first I had an actual widget component. However for this to work, I had to delete that component. I’ve tried for 2 hours now and I just can’t find a way to have it float above the enemy! I tried making a scene component and getting the world location, but that didn’t work. Then I tried converting it to player screen, but that didn’t work.
Since you’re using a widget component here, there’s no need to add the widget to the viewport - this will happen automagically. You will need to cast to whatever widget you asked the widget component to hold onto, of course.