Kill Oldest Object

Let’s say I’m creating a spawner that constantly spawns an object but increases in fequency depending on input from the player. The object already has a short life span, but I want to make it so no more that x amount can be alive at once. If the number of obect is greater than x, kill off the oldest one. What’s a basic way to go about this?

As you spawn the object, add it to an array. Then, every few ticks (be aware as looping through alot of objects can be performance heavy) do a ForEach loop on the array and check each object if it is still valid/not null (call IsValid), if it is not it means its already destroyed so remove it (via RemoveObject), after the loop body is completed, check if the length of the array is <= desired and if not ForLoop RemoveIndex(0) for how much the length exceeds the desired length.

EDIT: and actually destroy the object as you remove it with RemoveIndex(0) :D, forgot the most important part.

Hi KeoneShyGuy ,

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

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

@drUniversalis, Indeed :slight_smile:

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

It’s also a good idea not to call that every frame since looping through the array can take a while if the actor list gets big.

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

while this is fine if objects cant get destroyed, it will fail to gurantee that max actors are spawned when some of them get destroyed otherwise. you will still hold null references in the array and they only get destroyed when its their turn to be index 0.

you should add your second screenshot too :wink: (knew i saw that screen a few mins ago) and if you do, include a hint to not call that every game tick when maxactor count gets big.

This makes a lot of sense. I figured loops would be inefficient, just couldn’t think of a better alternative. Thanks.

this is a much more elegant solution then spawning, i like the idea of having an array with inactive objects in particular, how do you bypass updating indexi though if you perform an oldest object → newest object operation?

lets say your character can shoot 5 fireballs at a time. you could have an int called PoolIndex, which defaults to zero, and another int called maxPoolSize, which is set equal to 5 for this example.

the first 5 shots are done normally, spawning and filling up the list. the 6th shot realizes the array length is equal or greater than maxPoolSize, so it branches to a separate method.

that method would take the element at index PoolIndex, reset its state to default, move it to the spawn point, and give it the proper InitialVelocity, then it would increment PoolIndex, and check if PoolIndex is greater than MaxPoolSize, and if so, it subtracts MaxPoolSize from PoolIndex, and sets that as PoolIndex.

now, if those fireballs hit something or go beyond a certain range, they should change their state to inactive, instead of destroying themselves. the inactive state can set the objects visibility to hidden and zero out its position and velocity.

since the inactive elements are not destroyed or removed from the list, the list elements never have to change their index, and you never have to loop through each element asking it for a TimeStamp variable, and you never have to loop through to find which elements are inactive.

there are other, more complicated ways to implement an object pool, but if you want to overwrite the oldest element with the newest, you can use this simple method.