Use one Health Bar for several Enemies

Hi there!

I want to use ONE healthbar for several enemies. For that i created a widget component in the enemy_BP and created a Healthbar-Widget with binding the Progress Bar. But it only works for the first enemy and it reduce the healthbar of the other enemy at the same time. If the first enemy is dead the other enemy has a full healthbar and reduce progressbar by hitting him. What i am doing wrong? Is “GetAllActorofClass” the wrong way? I tried to cast in the GetPercent-Function to my Enemy_BP but dont know what to connect to the “Object-Pin”.

2 Likes

What do you mean exactly by “one health bar for several enemies”? You want one “global” health bar and each enemy reduces the total by a certain amount when killed? For example, 10 enemies, each when killed reduce the “enemy health bar” by 10 pts. So you have one bar with 100pts total and each killed enemy subtracts 10 from it so when you kill all 10 the health bar is empty? Or do you mean you want each enemy to have their own individual health bar but you want the health bar to be the same “class” for all enemies? Like each has their own personal health bar which only reduces when that particular enemy is hit but the “health bar” is a common thing for all enemies? Please clarify

Right, I mean the second thing. Every enemy should have its own health bar. But I want to avoid making a specific health bar for every single enemy. Otherwise I have 100 enemies at the end and have to make 100 healthbars. Is it possible to build something like a “universal” Healthbar?

Yes, you just make a widget, add a progress bar with a function that updates based on the input float value. Then in your enemy BP add a widget component, select the health bar widget (or whatever you called the widget with the progress bar), now you have a widget for each enemy instance. In your enemy BP script the logic to update the widget based on current health. So when the event is damaged, have a function that gets called which grabs the widget component, cast it to the health widget and call the function within the health widget that will update the progress bar.

I’m not sure I understood everything, but I’ll try, of course. I thank you for your help!

Hi, it’s me again. I tried to do everything like you said. But the last sentence of your answer is difficult to understand and something goes wrong in the healthbar widget. I made a reference to Enemy_BP with the name: “RefToHirsch”, but the variables have the value 0 instead of 100 during the game. I’m not sure which object is needed when i’m casting to Enemy_BP. I’ve added two more images. Maybe this explains my problem better.

And something else important for castto object-pin: the parent class of Enemy_BP is “NPC_Base” and that has “Character” as parent class. Seems to be important to know which object to use with castto Enemy.

So I have screen shots from my implementation of what I am trying to tell you. My “Master Enemy” has a widget component that is basically the “health”. Although mine uses a text to display health not a progress bar so I showed you the widget component on the master enemy and then the next 2 screen shots are showing a progress bar on my player HUD and its implementation but had the progress bar stuff been in my enemy widget everything would have worked the same. So ignore the fact that these are actually in 2 different widgets and pretend it’s the same widget. This is the setup I am trying to help you re-create.

Hey buddy, just did this myself too.

The problem in your picture is that you’re using get all actors of class. Which means all actors in that class will update the health value. Lemme show what I got.

So these are some sample enemies with healthbars:

266907-healthbars.png

][2]

As you can see it tracks individual health. I assume this is what you’re looking for?

This is the function for setting the health bar level:

So simple it’s almost stupid isn’t it?

Here’s how it gets sets when enemy is initalized:

It’s set to hidden in game until the enemy has a target because it’s set to rotate with the screen and unfortunately cannot be prevented from showing through walls (you don’t need this in the blueprint, just tick the box for the component). There are workarounds for this but they are time-consuming and I’m lazy. I just do show/hide instead.

EnemyHP is of course my widget blueprint. HP widget ref is a variable I made (drag out as enemy hp → promote to variable). And healthbar is the actual widget component attached to the enemy in the viewport.

I was gonna add how it’s updated when it takes damage but it’s kinda pointless since it’s the exact same (just the health variable is different).

If you have any questions be sure to fire away. The “Max health” variable is totally optional, you can divide by whatever you like to get it as a percentage (health points need to be between 0 and 1 in my version, but you can make it between any values you want in the widget blueprints).

The benefit of this is that it can literally be used for any enemy you have. If it’s a big enemy just scale it up and move it higher in the viewport for example, or you can set it to its feet. Or you can make the widget stick to your own screen if it’s a boss type where you want visible health on screen instead of over the enemy (I’d probably duplicate it and make some visual alterations just for looks in that case). For that kind of thing you’d use add to viewport instead though.

1 Like

You are using a “binding” property in the widget. Which runs on tick and is really over kill for something like health that doesn’t change that frequently. Your method works, but using an actual function call will help performance especially if you have a lot of enemies in the scene. What you said about the “get all actors” node is correct, I just wouldn’t recommend binding health in general.

If we were talking about doing this 5 years ago or not having any kind of possess function for our enemy AI, then yeah that would could have an impact, depending on basically everything else you do per tick. However it functions as a property binding, not a function binding so the impact is less severe.

Haha, yea I mean I wasn’t implying this was going to be a noticeable drop, just more on principle that if it doesn’t need to be updated on tick why do it? This by itself negligible, but since the OP is probably new there might be a bunch of things running on tick and this just seemed like an easy thing to keep off tick. Anyway, either way I am sure the OP will be fine, by the time whatever is being built is ready for prime time I am sure these things will be hashed out.

Side note…what are you creating up there? Is your character going to survive the goblin horde? haha

Sorry I made a mistake there, I don’t actually use a function to set the health level in the widget blueprint. It’s just a float variable, no function in the widget necessary.

As a bonus, you can use the same widget blueprint to set the names of your enemies:

266943-healthbars.png

Which is also set by just a variable you set in the character blueprint for the enemy/AI. You can also add icons for status effects, buffs etc. It makes it all super simple.

You’re welcome :slight_smile: Be sure to mark it as resolved!

Also, in case you missed the follow up comment, just go ahead and delete that function with the HPBind as it is unnecessary. A variable is enough.

Thank you both for your help. I have now used Crowley’s suggestion, seemed to be easier. Now it still works! Just do a few fine tunings, because I have a huge mess of variables in my blueprint. Thanks again!

best regards from Switzerland!