Make enemy health bars independant?

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.

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.

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.

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.

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.)

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.

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.

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

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.

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

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

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

Love and respect,
Dan

Thank youuuuuuu!!!
This fixed my problem as well :slight_smile:

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

Excellent. Good luck!

L&R

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