Destroy an actor after it has spawned

Hi all,

So I basically have an event tick wired into a spawn actor node, as shown below:

Please ignore all the if statement, that’s just logic to how it is spawned.

Eventually it’ll just keep spawning and spawning actors which might overload the CPU eventually.

I’d like to destroy the actor a few minutes after it’s spawn, how would I do this?

Keep a reference to them in an array object.

Then iterate the array and kill off the ones at the end. Essentially a FIFO queue…

So just create an array of spawned objects, add each object into the array when it spawn and then what? what nodes should I use?

Sorry I’m a bit confused.

Also what should I do if it’s destroyed from another blueprint? How would I remove the item from the array?

Well if you want to destroy the actor in a fixed time intervall after spawning it, you could use this approach.
Otherwise you can always call the event manually when you want to destroy one.

Note that the Actor Spawned Event has to be replaced with the appropriate one, i was just lazy and made a custom event there.
I am also not quite sure how Blueprint does handle its arrays, you would have to test if the “Remove Index” Node is neccessary after destryoing the actor
(i suppose Remove Index does reduce the size of the array afterwards whereas destroying the actor does not)

Hi ShrewGlue,

One way you could do it, is instead of doing it based on time, is doing it based on a max number of element.

If your concern is purely the maximum amount of actors that can exist at one time this solution would work great.

In the blueprint below I spawn an actor every tick, add it to a list of existing actors, and then I validate if there’s more actor than “MaxActor”, a variable I can edit depending on performance.

If I have above MaxActor elements, I removed and destroy the oldest one.

Hope this helps

  • Marc

http://s18.postimg.org/yfluem7ll/Remove_Elements.png

This is perfect; one last question: What if one of the actors in the array are destroyed by another blueprint?
How would I update this blueprint and get the destroyed actor out of the allCurrentActors array?

careful as this will not work as intended. delays behave unintentional when called multiple times in succession and it will block all calls until the first delay is over…then one call can pass through and again all future calls will simply not be executed. Say you spawn 10 objects in 2 seconds, only one will get destroyed.

Even though the First in first out (FIFO) is better for controlling lag due to multiple spawns, if you somehow still want your timed approach:

  1. Go into the to be spawned object.
  2. add Event Begin Play Node → Delay → Hide/Destroy actor (In single player you can just call destroy actor, in multiplayer you should hide it and call “set lifespan” with one second to give the object time to syncronize to clients)

be sure to trigger this delay in the spawned object and not after your spawn node as it wont work (see my other comment)

1 Like

You could add a bit of blueprint like this one after adding an actor to the list.

In between the Add and the Branch it’ll check for deleted actors and remove them from the list. That way you will not delete old ones if it’s not necessary

http://s22.postimg.org/pd3ae184h/Remove_Part2.png