Currently I have to save the list of all the actors, and compare it every frame to see if an actor has spawned or been destroyed.
There must be a better way?
Currently I have to save the list of all the actors, and compare it every frame to see if an actor has spawned or been destroyed.
There must be a better way?
Surely it’d be better to use event dispatchers and bind to OnDestroyed for cleanup. Unfortunately, you’ll have to manually register the newly spawned actor with your manager. You can’t bind to ‘OnSpawned’ so when you do spawn the actor, make sure to notify the manager. Curious as to what you’re getting over ‘GetAllActorsOfClass’ especially if you’re updating every frame instead of when it changes. I guess this way you aren’t calling it in multiple places.
Well, mostly its for the UI to display health-bars over the characters. In my HUD widget, I create a health-bar for each character, and I store them in an array and update them every frame. I need to know when a new character has spawned, or one has been destroyed for this to work though.
Perhaps there is an overall better way?
Are you doing this in UMG or an actual HUD object?
If you were doing this in an actual Hud object, it’d be as simple as ‘GetAllActorsOfClass<type>’, and then running the health bar logic for each.
Doing this in UMG would work, but you’d clear all widgets, and then create widgets for each actor. You surely don’t need to curate a list.
That’s if you wanted simplicity at the cost of performance.
If not, you’d do this.
Make a dispatcher in your unit for when he’s destroyed.
Have a managing actor, ‘Unit Tracker’. Spawn Unit, call function ‘Unit Spawned’ in ‘Unit Tracker’. This function adds unit to the array and binds an event to ‘IWasDestroyed’ for that unit. When the unit is destroyed, ‘Unit Tracker’ receives a notice and removes it from the list.
Unit Tracker can also be in your HUD/widget, though your spawning logic would have to have a reference to the widget.
I’m doing it through a UMG Widget.
Thanks for your help, this is enough to fix my problem.
As a side-note I have to curate a list because the health bars include animations and whatnot so must be assigned to one character for the duration or the animations would fail.
EDIT:
One question though, is it okay to create potentially hundreds of UMG widgets every frame like you’re saying?
You would probably get better results moving them, and then recycling them if you have any free.
Definitely don’t create hundreds every frame. You’re asking for slowdown. UMG widgets can’t be explicitly destroyed, only removed, then they’re garbage collected later. You’re much better off giving each character its own widget (like a 3d widget component) or like said, recycling them based on whats rendered. Either way you probably don’t need to update them every frame, only when the health is being affected by damage or healing.
The way I solved the issue is by using a blueprint with just a sphere that generates overlap events as a ‘sensor’ that tells the HUD when someone is within range or leaving range.