# Make enemy health bars independant?

**URL:** <https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [September 12, 2016, 10:41am UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181 "2016-09-12T10:41:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jarrastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jarrastafari](https://forums.unrealengine.com/u/Jarrastafari)\
**Post date:** [September 12, 2016, 10:41am UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/1 "2016-09-12T10:41:15Z")

</div>

Hi guys, just a quick one. I have an enemy (_BossAI_ blueprint) character actor that has a widget component whose widget class (_EnemyVitals3DW_ user widget blueprint) shows the enemy’s name and a health bar.

 ![106622-sharedhealthbars.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/b/4/7b4d679ccb3cc28ee010be703e43003eedf34cd1.png)

The problem I have with my implementation is that if I duplicate the enemy blueprint all of the enemies share their names and health bars with the first instance of the enemy. Though I have tested and each enemy has their own health and can be destroyed independently. I feel like I know what I need to do, to tell the ‘cast to’ node to select the correct instance of the enemy that corresponds to itself, but I can’t for the life of me figure it out.

Sorry if this is a simple fix but I would really appreciate it, because as soon as I feel I have the hang of blueprint communication I always hit a stumbling block such as this.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d24c3c621](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d24c3c621](https://forums.unrealengine.com/u/anonymous_user_d24c3c621)\
**Post date:** [September 12, 2016, 11:30am UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/2 "2016-09-12T11:30:16Z")

</div>

Hi,

Why do you you iterate on all actors of your BossAI class to update UI ?

You should directly your blueprint widget component from your HP and Name variables.  
This implementation is more optimized and easier to debug.

Otherwise, your problem is you never check the current element in your array is your current actor. The Cast return the object with the type you asked if possible, it doesn’t check if your instance is the same as the Self instance.  
You must check Array Element is equals to Self reference.

But again, I don’t recommend you to keep continue using your implementation.

---

<div class="post-metadata">

**Author:** ![Jarrastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jarrastafari](https://forums.unrealengine.com/u/Jarrastafari)\
**Post date:** [September 12, 2016, 11:40am UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/3 "2016-09-12T11:40:55Z")

</div>

Thanks for the reply, HellSuffering.

I am happy to cut out the array, as I simply used it to get an object reference in the first instance.

 ![106627-sharedhealthbarsfix.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/4/5/b45709cefeb2ba39ab283af24bc6e92f7be08588.png)

Could you please state which node(s) I can use with the Cast To object reference that would identify the BossAI that each user widget is associated with? I have tried plugging in everything I can think of (e.g. get user widget object, etc.)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d24c3c621](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d24c3c621](https://forums.unrealengine.com/u/anonymous_user_d24c3c621)\
**Post date:** [September 12, 2016, 12:16pm UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/4 "2016-09-12T12:16:52Z")

</div>

You don’t need to Cast anything, Cast is just a type conversation to get an object of a type to another compatible type (main key of polymorphism in object oriented langage).

HP and HP max are variables on your blueprint, you just have to return HP / HPMax because these variables are owned by your blueprint instance. You just have to link it to your widget progress bar percent propertie. So each instance of your blueprint have their own HP and HPMax vars.

---

<div class="post-metadata">

**Author:** ![Jarrastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jarrastafari](https://forums.unrealengine.com/u/Jarrastafari)\
**Post date:** [September 12, 2016, 1:25pm UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/5 "2016-09-12T13:25:06Z")

</div>

Very grateful for the advice.

Unfortunately I hold the HP and HPmax values in the BossAI (enemy character) blueprint, so to access them I would have to cast (no?). If I remove the cast and just select _get HP_ and _get HPmax_ nodes from the BossAI blueprint, I end up with an error. Which makes sense to me, as I haven’t setup any communication between the two.

 ![106629-sharedhealthbarssimplified.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/3/8/438ddfc55af96c9dfd0ecff80219b9ac3cec2775.png)

I am reviewing the [Blueprint Communications][2] video now, but I feel like I know what I want to do but I am missing something obvious. Perhaps I have misunderstood you.

* * *

To be clear, I have:

- BossAI (the enemy BP) storing HP and HPmax values as Int
- a user widget (_EnemyVitals3DW_) that draws a health bar and name plate for the enemy
- a widget component in the BossAI, with it’s widget type with it’s widget class set to _EnemyVitals3DW_

And I want to:

- take the HP and HPmax variable values from the BossAI blueprint
- get them in the EnemyVitals3DW widget blueprint
- calculate the percentage and feed that into a progress bar
- have multiple BossAI characters in the level at the same time with their own health bars

* * *

 ![106630-sharedhealthbarsproblem.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/7/a/27ad7bfdfbd7a23eb4a0cd6a8b90fc12ee9c4e59.jpeg)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d24c3c621](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d24c3c621](https://forums.unrealengine.com/u/anonymous_user_d24c3c621)\
**Post date:** [September 12, 2016, 2:10pm UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/6 "2016-09-12T14:10:05Z")

</div>

I have not understooden you were in the Widget Blueprint.

I don’t know very well how to structure blueprint as I almost use C++, but you can try to make Setters on your widget blueprint (method like SetMyVar(int Value)). Then, from your character blueprint, use this setter on your widget component to communicate the value.

---

<div class="post-metadata">

**Author:** ![Jarrastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jarrastafari](https://forums.unrealengine.com/u/Jarrastafari)\
**Post date:** [September 12, 2016, 8:01pm UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/7 "2016-09-12T20:01:41Z")

</div>

Ok, so since I couldn’t figure out how to cast to an instance of an enemy and have their health bar display their own health and not their progenitors, I have explored and combined two different approaches to achieve the same result.

If anyone gets as stuck then please see the following two tutorials:

- [Blueprint Communications (@~1:34:00)][1] by Zak Parrish from Epic
- [Unreal Engine 4 Tutorial - 3D Health Bar (NPC’s)][2] by Tesla Dev

 ![106690-solution.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/4/8/5486eee7330d85074a253c9a71e67d04b63d3345.jpeg)

First, I decided to use event dispatchers, as explained by Zak, to update my player characters HUD. Even though I felt I was completely familiar with casting and interfacing with blueprints (obviously I wasn’t) I found the whole video is a great refresher, as he does well at explaining the different methods of blueprint communication and their uses.

Second, I used Tesla Dev’s method for my enemy HUD and found that each instance of the blueprint would read their own health correctly and so resolved my main issue.

Lost 10 hours of work today tackling this issue, but after rewatching Zaks video and working through to a solution I feel my understanding of the engine has improved. Overall it has been worth the effort and the stress. Hope this helps advice can help someone.

Love & respect, Dan

---

<div class="post-metadata">

**Author:** ![Bluemak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bluemak](https://forums.unrealengine.com/u/Bluemak)\
**Post date:** [April 7, 2017, 9:55am UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/8 "2017-04-07T09:55:40Z")

</div>

Thank youuuuuuu!!!  
This fixed my problem as well 🙂

---

<div class="post-metadata">

**Author:** ![Jarrastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jarrastafari](https://forums.unrealengine.com/u/Jarrastafari)\
**Post date:** [April 7, 2017, 10:15am UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/9 "2017-04-07T10:15:58Z")

</div>

Glad I could help. Stick with it and you can succeed.

Love and respect,  
Dan

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_09425f7d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_09425f7d](https://forums.unrealengine.com/u/anonymous_user_09425f7d)\
**Post date:** [August 30, 2018, 6:33pm UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/10 "2018-08-30T18:33:50Z")

</div>

Thank you for the link on BP Communications, it’s been incredibly informative.

---

<div class="post-metadata">

**Author:** ![Jarrastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jarrastafari](https://forums.unrealengine.com/u/Jarrastafari)\
**Post date:** [August 31, 2018, 4:14pm UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/11 "2018-08-31T16:14:35Z")

</div>

Excellent. Good luck!

L&R

---

<div class="post-metadata">

**Author:** ![GeorgePopa123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GeorgePopa123](https://forums.unrealengine.com/u/GeorgePopa123)\
**Post date:** [October 23, 2019, 3:17pm UTC](https://forums.unrealengine.com/t/make-enemy-health-bars-independant/370181/12 "2019-10-23T15:17:08Z")

</div>

Hey, man, can you show like a blueprint or something because I can’t get it to work
