Destroying the oldest actor in an array sometimes destroys the newest instead?

Hey, pretty new to BP and Unreal in general, and most issues I’ve run into I’ve been able to figure out with trial and error and google, but this one’s got me stumped.

In my project I’m currently using the default First Person template, where I’ve made some slight adjustments.

The projectiles are now permanent, and in the first block shown I limit the amount of concurrent projectiles in the scene to 5.

In the second block, upon attempting to create a sixth projectile, Unreal will destroy and remove the oldest projectile using index 0 before approving the creation of a new projectile.

This current setup works perfectly about 85% of the time, with, beginning with the sixth shot, the oldest of the five being destroyed to spawn a new projectile, which means the player only has the most recent 5 shots in play at any given time.

But seemingly randomly there will be periods where, instead of destroying the oldest object, the newest projectile is destroyed instead. This leads to a period of anywhere between 15 seconds and a full minute where the oldest 4 projectiles remain in play and the newest instance keeps getting overridden instead.

This bug is guaranteed to occur at least once during a 5-10 minute play session, but I can’t seem to narrow down the cause or replicate the conditions. Sometimes it happens after I shoot 50 shots, sometimes it happens after I’ve only shot 15. I’ve tried moving and standing still, shooting different objects, shooting at different intervals, I’ve got nothing.

The only changes I’ve made to this mechanic are shown in the attached screenshot, so it’s got to be in here somewhere, but I don’t see it. Maybe I’ve constructed an incorrect way to approach this issue? I’m not sure. Anyone have any ideas?

Get All Actors of Class does not guarantee actor order. Use it only when you truly need to do something to ALL actors, when you need to pick a random one or when you have just 1 actor. Or even better, don’t use it at all :slight_smile:

Instead, create an array for your actors and keep them there. As you spawn projectiles, add them to this array. This way, the most recent will always be at Last Index and the oldest at Index 0.

1 Like