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)
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.
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:
Go into the to be spawned object.
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)
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