Remove from array is not working

I don’t know if this is a bug or I’m doing something wrong.
I have a struct list that stores which characters follow which. I can add to this struct but when I try to remove characters, it only removes half at a time.

This is the code:


The struct is “Follow List” and is just an array of 2 actors. The struct takes in the “Leader” and the “Followers” like so:

Leader 1 | Follower 1
Leader 1 | Follower 2
Leader 1 | Follower 3
Leader 2 | Follower 4
Leader 2 | Follower 5

And the idea for stopping the followers from following was to remove the leader entries.
But for some reason, it only removes half of the entries at a time.

The way I’m working around this is by running it 5 times in a row.

Is this a bug?

You should remove elements in reverse order.

My Products

3 Likes

It is not a bug, you cannot remove an item that is in the middle of an array, when you remove an item, what the array does is reorder the IDs, for example id1=A, id2=B, id3=C, Id4= D, if u remove “B” now u array is id1=A, id2=C, id3=D

If you want the Id to be permanent and you don’t want these problems, you can use a dictionary that in unreal engine is called “MAP” or delete from the last Id

3 Likes

Ok thank you, I didn’t realise that it worked like that :sweat_smile:

I have the same problem but with CLEAN. I tought when you clen an array, it’s empty.

Removing items that are in the middle of the array is generally not a good idea. There is a node called “Swap Away Array Elements”. Use it to reorder your array so that the element you want to remove becomes the first element of the array. Then, use the Delete Index node to delete index 0 of the array. That way, your array won’t get reordered unintentionally.

If you need your array to have a specific order, you can use the “Set Array Elem” node to expand your array into adding an empty array element. Make sure to check its boolean, and add your empty variable to the length of your array with “Length” node. Then, you can use access it by getting your array’s last index with the “Last Index” node, and swap that with the element you want to remove. Afterwards, delete this one too with the “Delete Index” node. You can do it by running a “Reverse For Loop” on your array. Inside the loop, use the “Find” node to find your empty element’s index. Then, run a Branch node to “Remove Index” inside the loop to delete the index of the empty element in the middle of the array. If you want to reuse it, add the empty element to the length of your array again the same way. This way, everything will revert to the same order, and you’ll get rid of the specific array element.