Update Variable from randomly spawned Actors

why are the npcs not creating their own healthbars? your database actor is not a player so your last picture would never work. also the get all of class then get index 0 is about the worst way to get a reference to something, there is a better way to go about it but it will depend on your situation.

anyways the way i would go about this is to have the widget as part of the npc and have the progress bar in the widget auto update based on a binding.

to accomplish this we will need to have a widget that has a progress bar and a variable that will be a reference to the character. as you can see in the picture below i have a variable names CharRef that is of the thirdperson character type which will be set later. the picture also shows how we will be updating the value displayed of the healthbar via the binding. for the binding we are just getting the health variable from the character reference the normalizing the value so we get a 0-1 value.

in the character we will simply add a widget component and specify the widget class to use in the details panel. then we will also need to set the variable that is the character reference in the widget. this is done on begin play we get out widget component, then cast to identify it, and finally drag off and set the needed variable.

this is the basics of adding a healthbar to npcs and there are many other options to implement if you so choose, for example you could have the widget only visible when the npc has perceived the player. now i may have been off the mark on what your asking and it seems like theres more to what your asking but your question wasnt all that clear so if theres something i missed let me know and ill try to address it.

Hello !

I have a script where I retrieve all the NPC spawned that are in combat with the player, then procede to retrive theirs variables.

This work ok

However I am attempting to add Ui Health bar to them (One that update when Hp change for instance) and I do not know how to proceed.

A: Should I attempt to Set a variable whiten the actor that have spawned?
I do not know how to do that (seem my method to retrieve variable, only retrieve a copy of the values)

B: Should I try to spawn the UI separately and attach it to the spawned actor (Also I do not know how to do it ATM)

Is there a better option?

why are the npcs not creating their own healthbars?

That what I current got, but I am not able to update it after they get spawned! (A)

(You probably will dislike the event tick ??)

Therefore it get me to think that maybe, instead of updating the actor, to spawn theirs Ui then Add it to them (B)

As I said, probably, because as you said get actor of all class is not a wonderfull idea, However that the only I got/know so far to retrieve the NPC I spawn from various spawner, (thoses have different list of npc to spawn but all from the same class)

So my problem is not so much creating the health bar or filling it. It is more getting the reference to the actor I spawn to update its variables.

Hope that this is clearer

Thanks

More information about the spawning process (not saying its good but at least you might understand better the way it is organized)

if each npc has its own health value then theres no need to reference them from an outside source, they can all handle their own health and damage systems and update their own health. in the example i provided above i used a binding for the progress bar, using the binding the way i did will enable the value to automatically update based on the variables provided. basically when the npc is created i told the widget who the npc was (gave a reference) so that it could track its values.

but if your deadset on telling the widget when it can update then you need a reference to each npc you want to call the event in and they all need to have the same event to call. theres a few ways to do this and it depends on how you want to target each npc. if you want to tell all the npc in the level to set percent then you would use the get all of class to get an array containing every npc then you would use a for each loop to call the event on each npc (this could also be done with any array containing the references). now if you wanted to target a single npc then you would need to use a different method to get the reference such as a trace, overlap event, or a public variable. these three methods will return a actor reference but they all have specific uses of which none really apply to your situation well. another option would be to have the npc update whenever it takes damage which would look something like event any damage-> calculate health → call update healthbar event.

the system your currently trying to use doesnt make a ton of sense to me, it looks like your storing all the information about the npcs in another actor and using that actor as a middleman for everything.

oh and your right about what i think of tick. how many times per second does the health need to be updated 30 60 120, its a bit much, i would much rather only update it when needed.