Add Array Replaces Stuff Instead Of Adding To Array

I have a BP where I want a cube to spawn every time I press “H”.

The main code is placed inside the Actor that should be spawned, and to make the code run, one instance of this Actor is already inside my scene in the game in a hidden form.

The code works as intended and every time I press “H” a new cube spawns and follows the Spline through a Timeline. (I’m using a Dispatcher from my PlayerCharacter to the Cube to run the code whenever player presses “H”).

The only problem is that my AddArray node does not work as intended. I have put AddArray right after SpawnActor, so that it stores every spawned Actor inside the Array and then I could use that array somewhere else to adjust some values.

However, AddArray node works more like Set Array Element. Every time it runs, it removes the previously stored Actor at Index [0] and stores the newly spawned Actor at the same Index.

I put a Breakpoint right after the AddArray node to check the stored Actors. There is only one index every time inside the Array while I have multiple cubes on the scene. However, the Actor inside that index is always the latest spawned cubes.

Somehow, the cubes that have been spawned earlier, do not stay inside the array.
I tried using AddUnique and Insert nodes instead of Add, but none of them fixed the issue.

Here is the code (from Hpressed Event):





in my level I have some actors that are of type BP_ItemPickup (what this class is doesn’t matter, but for the purpose of this they derive from AActor)
named: BP_ItemPickup00, BP_ItemPickup01, BP_ItemPickup02, BP_ItemPickup05

I gave one of my blueprints I created an array of AActor* and 4 pointers to Actor marked them all as public (the open eye symbol)
in the level I assigned my actors in order to the exposed pointers on the object
then created a function

then in the event graph did the following

when I hit play my log is this

add_: 1
0: BP_ItemPickup00
add_0: 2
0: BP_ItemPickup00
1: BP_ItemPickup01
add_1: 3
0: BP_ItemPickup00
1: BP_ItemPickup01
2: BP_ItemPickup02
Insert_2 at 0: 4
0: BP_ItemPickup05
1: BP_ItemPickup00
2: BP_ItemPickup01
3: BP_ItemPickup02

this is the intended behavior of “insert_Single”: increase the arrays size by 1, shift all elements after by 1 place then place the new element at the designated position.

note that if you are doing multiple inserts/deletes the order of the array may effectively become random (especially for complex object types), to maintain a strict order of an array you would have to implement a sort (this is not “easy” in blueprints without plugins)

for your given situation the Entity might not fully exist by the time you are inserting it into the array. I would try assigning the spawned actor to a local variable (to the object if not a function, and or an actual local for a function0, then do stuff on that before just using add. to do an IsValid check on the actor before accessing it.