# Removing single element from array removes wrong element (UE5.4.4)

**URL:** <https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114>\
**Category:** Blueprint\
**Tags:** bug, unreal-engine, Array, remove, player-start, Blueprint, question\
**Created:** [September 29, 2024, 6:56pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114 "2024-09-29T18:56:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![IronAttorney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IronAttorney](https://forums.unrealengine.com/u/IronAttorney)\
**Post date:** [September 29, 2024, 6:56pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/1 "2024-09-29T18:56:53Z")

</div>

I have an array of 16 player starts. I am choosing random `PlayerStarts` from the array to spawn in an actor per player in response to `OnPostLogin()` in my `GameMode`. I remove that `PlayerStart` from the array each time.

Later in repsonse to a HUD button press by the host player, I spawn in neutral actors in each of the other `PlayerStarts` remaining in the array.

The problem is, that the removed `PlayerStarts` do not correspond with the ones the connected players already claimed, infact it seems entirely random which elements get removed. The correct amount are removed, I’ve checked that thoroughly, but not the correct elements.

Below is the macro getting a random PlayerStart showing two methods I’ve tried that should both achieve the same thing:

 ![UE5 Remove Element Bug 00](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/e/4/ee421ad911a36a23cccd3904d3c6343c602f005c.png)

Here is where that macro is executed and then the returned data is used to spawn the player city actor, and remove the `PlayerStart` from the list. I’ve tried both of the array removal methods with same buged results:

 ![UE5 Remove Element Bug 01](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/2/b/82bf86886495b27fd4184a4ebadc274ce27bbd50.png)

Then later this is called to fill the rest of the `PlayerStarts` with neutral unclaimed cities, triggered by a UI button press (and definitely way after the initial palyer cities are added) preceeded by a check of the array lenght to clarify the correct amount were remove:

 ![UE5 Remove Element Bug 02](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/6/2/b62b3ec4d7f924d93a4ddac27b4b204bb3c881f4.png)

After playing and pressing said button, I can click through the player starts and clearly see a lot of the time that some have no cities associated with them, and the ones with the player controlled cities often have neutral cities spawned on them aswell (the exact proportions of each issue balancing out to make 16 cities in total each time which is the correct amount). I’ve spent a while on this, and I can’t see a good reason why this simple operation should go wrong in this way.

It would obviously be insane for the `array::remove()` functions to be wrong after so many years of UE5 blueprints through so many versions, but I’m struggling to see what else it could be that fits the nature of this bug

---

<div class="post-metadata">

**Author:** ![PREDALIEN](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/predalien/32/1194670_2.png) [@PREDALIEN](https://forums.unrealengine.com/u/PREDALIEN)\
**Post date:** [September 29, 2024, 7:14pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/2 "2024-09-29T19:14:01Z")

</div>

The problem (or rather the feature) is that “pure” nodes (without execution pins), including “random”, are executed every time a value is needed somewhere. So you get two different values ​​of “random”.  
You need to cache “random” and then use that value.

---

<div class="post-metadata">

**Author:** ![IronAttorney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IronAttorney](https://forums.unrealengine.com/u/IronAttorney)\
**Post date:** [September 29, 2024, 7:17pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/3 "2024-09-29T19:17:01Z")

</div>

Ah. I had that in mind when I collapsed the random getting part into a dedicated macro… I assumed the macro would have the random value cached already after execution. Is that not the case?

---

<div class="post-metadata">

**Author:** ![IronAttorney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IronAttorney](https://forums.unrealengine.com/u/IronAttorney)\
**Post date:** [September 29, 2024, 7:20pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/4 "2024-09-29T19:20:55Z")

</div>

Ok, I think the accepted answer here covers that:

> [@What's the difference between Blueprint Macros and Blueprint Functions?](https://forums.unrealengine.com/t/whats-the-difference-between-blueprint-macros-and-blueprint-functions/280809):
>
> They both seem to do the same thing in the end. When should I use a function and when should I use a function?

And now I understand finally why they’re called macros! That C/C++ name never made sense to me before, haha

---

<div class="post-metadata">

**Author:** ![PREDALIEN](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/predalien/32/1194670_2.png) [@PREDALIEN](https://forums.unrealengine.com/u/PREDALIEN)\
**Post date:** [September 29, 2024, 7:22pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/5 "2024-09-29T19:22:56Z")

</div>

No, during the compilation of a blueprint, instead of the macro, all the nodes that were inside it are created, and the macro itself is deleted.

Standard functions (not pure) cache the result, and it will be updated the next time the execute pin is called.

---

<div class="post-metadata">

**Author:** ![IronAttorney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IronAttorney](https://forums.unrealengine.com/u/IronAttorney)\
**Post date:** [September 29, 2024, 7:27pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/6 "2024-09-29T19:27:57Z")

</div>

Thanks very much! That saved me now and from making similar mistakes in the future

---

<div class="post-metadata">

**Author:** ![KassidyDavila](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kassidydavila/32/982474_2.png) [@KassidyDavila](https://forums.unrealengine.com/u/KassidyDavila)\
**Post date:** [September 30, 2024, 1:38pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/7 "2024-09-30T13:38:33Z")

</div>

In Unreal Engine (UE5.4.4), when attempting to remove an element from an array and the wrong element is removed, this is often related to the following common causes:

### 1. **Index Mismatch:**

- Arrays in Unreal Engine are zero-indexed, meaning the first element has an index of 0, the second has an index of 1, and so on.
- If you’re removing an element by index (e.g., `Array.RemoveAt(Index)`), ensure that the `Index` is accurate. If you’re using dynamic logic to get the index, debug it to confirm it’s returning the expected value.

**Solution:**

- Double-check that the index you’re passing is correct. Log the index before the removal to confirm.

```cpp
UE_LOG(LogTemp, Warning, TEXT("Removing element at index: %d"), Index);

```

### 2. **Array Modification During Iteration:**

- If you’re modifying the array while iterating through it (e.g., in a loop), this can cause issues. Removing elements shifts the remaining elements, which can result in skipping or removing incorrect elements.

**Solution:**

- If you’re modifying an array while iterating, consider iterating backward, as this prevents shifting issues:

```cpp
for (int32 i = Array.Num() - 1; i >= 0; i--)
{
    if (ShouldRemove(Array[i]))
    {
        Array.RemoveAt(i);
    }
}

```

### 3. **Removing by Value:**

- If you’re using `Array.Remove(Value)`, ensure that the `Value` you’re passing is correct and that the type matches.
- Unreal Engine’s `Remove()` method will remove the first occurrence of the element that matches the value.

**Solution:**

- Double-check that the value you’re passing in `Remove(Value)` is the one you expect to remove. Also, ensure that any potential type casting or conversions are handled properly.

```cpp
MyArray.Remove(ExpectedValue);

```

### 4. **Reference vs Value Types:**

- If your array contains pointers or objects (not primitive data types), there can be confusion between object references and values. Make sure you’re comparing the actual object references when removing or using custom comparison logic.

**Solution:**

- For complex types like structs or objects, ensure that the comparison operator (`==`) behaves as expected, or define a custom comparator if necessary.

### 5. **Async Tasks or Threads:**

- If you’re running asynchronous tasks that modify the array, there’s a chance of race conditions where the array is modified at an unexpected moment, leading to unexpected behavior when elements are removed.

**Solution:**

- Ensure that all operations on the array are properly synchronized or occur on the main thread where necessary.

### Debugging Tips:

- **Log the Array Contents:** Before and after removal, log the array contents to ensure you’re removing the correct element.

- **Breakpoints:** Set breakpoints in your code before the removal operation to inspect the current state of the array and the index or value being passed for removal.

By carefully verifying the index or value and ensuring the array isn’t being modified unexpectedly, you should be able to resolve the issue of removing the wrong element.

---

<div class="post-metadata">

**Author:** ![PREDALIEN](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/predalien/32/1194670_2.png) [@PREDALIEN](https://forums.unrealengine.com/u/PREDALIEN)\
**Post date:** [October 1, 2024, 10:00pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/8 "2024-10-01T22:00:37Z")

</div>

I have a weird feeling that this was written by a chatbot… 🤔  
Anyone else have the same?

---

<div class="post-metadata">

**Author:** ![IronAttorney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IronAttorney](https://forums.unrealengine.com/u/IronAttorney)\
**Post date:** [October 4, 2024, 2:02pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/9 "2024-10-04T14:02:10Z")

</div>

Yeah, it does read alot like the kind of thing chatgpt has been generating when I’ve posed it unreal engine issues

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [November 3, 2024, 2:02pm UTC](https://forums.unrealengine.com/t/removing-single-element-from-array-removes-wrong-element-ue5-4-4/2045114/10 "2024-11-03T14:02:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
