Add actors to array, then at array limit, destroy first actor.

Hello UE Forums,

I have 2 blueprints I am working with.

The first blueprint is some breakable glass, when the glass is broken it will add itself to an array in the second blueprint.

The second blueprint contains the array and an integer for counting how many times an actor is added to the array. When it becomes greater than 4, It will decrease the counter by one and destroy the first actor in the array and continue to the next one. (This is for optimization purposes to ensure there is not too much broken glass being rendered)

What I can’t figure out is how to destroy the 1st actor in the array, remove it from the array and then cycle to the next one.

In short: Break Glass > Add to array in another BP > Add 1 to counter > When counter hits 5 Destroy 1st actor in array > Remove Actor from array > Add 5th actor into 1st array slot > Get ready to destroy and remove 2nd array slot > Cycle through.


I get an error when trying to destroy the first item in the array and it won’t seem to cast to the actor to call an event for destroying itself either.

Any help would be much appreciated.

Just keep track of the last index added to Destroy and overwrite:

1 Like

It’s been a minute, but I could have swore Arrays auto shifted indexes in UE.
For example if you remove an index (say 2) the array would shift all the elements down the line, thus refilling index 2.

  • Index 3 → 2
  • Index 4 → 3
  • Index 5 → 4
  • etc.

If that’s not the case a Shift function is pretty easy to set up.

This will keep the oldest element at index 0

1 Like

Just confirmed Arrays do shift.

example Int array

  • index[0] → 34
  • index[1] → 21
  • index[2] → 56
  • index[3] → 73
  • index[4] → 5

Remove Index[0], Array is then shifted to:

  • index[0] → 21
  • index[1] → 56
  • index[2] → 73
  • index[3] → 5

Remove Index[2], Array is then shifted to:

  • index[0] → 21
  • index[1] → 73
  • index[2] → 5

@Z3R0K0
You do not need to cast the element in the array. Destroying an actor only requires an actor reference. It doesn’t care what the actors class is. Being technical the array should be of type Actor Obj Ref.

That being said, Add an actor reference to the Glass Array when one is broken. Immediately check the Length of that array. If it’s Equal to your control N, then Destroy index[0], remove index[0].

Index[0] will always be the oldest element in the array.

The idea behind what I posted was to avoid shifting and resizing the array twice every time a new glass is broken. :face_with_diagonal_mouth:

1 Like

I gotcha, it was solid. Just providing a different perspective.

End of day the engine shifts them on removal.

The index is not removed when the referenced actor is destroyed. Right?

No I’m saying when you “REMOVE INDEX” from the array. Which is what should be done when the actor referenced is destroyed.

1.) Is this an object refence to my BP that I am adding to the array?

2.) How do I create this node?

This works, sort of. It seems to be destroying the last actors added to the array instead of the first.
It will keep destroying the most recently added but leaves the first 5 on scene.

  1. Yes.
  2. Right click and type Math Expression node.
1 Like

Works perfectly, just had to enter the expression on the details panel and change the Inputs / Outputs to integer.

This is going to have a lot of uses in my project, Thank you.

1 Like