Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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". alt text

alt text

Product Version: UE 4.19
progress-bar.png (266.5 kB)
enemy.png (333.9 kB)
more ▼

asked Jan 28 '19 at 05:06 PM in Blueprint Scripting

avatar image

14 2 2 3

avatar image Nebula Games Inc Jan 28 '19 at 05:31 PM

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

avatar image HeroDave Jan 28 '19 at 06:44 PM

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?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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:

![alt text][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:

alt text

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

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

alt text

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.

[2]: /storage/temp/266907-healthbars.png

healthbars.png (453.2 kB)
healthbars.png (453.2 kB)
healthbars.png (26.3 kB)
healthbars.png (96.1 kB)
more ▼

answered Jan 30 '19 at 12:04 AM

avatar image

Crowley Caine
693 8 10 16

avatar image Crowley Caine Jan 30 '19 at 01:04 AM

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:

alt text

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.

healthbars.png (476.0 kB)
avatar image Nebula Games Inc Jan 30 '19 at 01:13 AM

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.

avatar image Crowley Caine Jan 30 '19 at 01:53 AM

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.

avatar image Nebula Games Inc Jan 30 '19 at 02:02 AM

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.

avatar image Nebula Games Inc Jan 30 '19 at 02:05 AM

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

avatar image HeroDave Jan 30 '19 at 09:37 AM

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!

avatar image Crowley Caine Jan 30 '19 at 09:49 AM

You're welcome :-) 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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Jan 28 '19 at 07:11 PM

avatar image

Nebula Games Inc
5.4k 28 9 15

avatar image HeroDave Jan 28 '19 at 08:25 PM

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

avatar image HeroDave Jan 29 '19 at 11:58 AM

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. alt text

enemy-bp.png (342.7 kB)
progressbar.png (400.3 kB)
avatar image HeroDave Jan 29 '19 at 04:57 PM

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.

avatar image Nebula Games Inc Jan 29 '19 at 11:30 PM

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.

alt text alt text alt text

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question