# Array has changed during ranged-for iteration!?

**URL:** <https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, question, CPP\
**Created:** [August 12, 2022, 10:16pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369 "2022-08-12T22:16:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 12, 2022, 10:16pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/1 "2022-08-12T22:16:40Z")

</div>

what is the cause of this breakpoint? Currently my array has the size `= [100]` and returned this error on beginplay… but if I increase the size to `= [200]` everything works fine.

```auto
Ensure condition failed: Lhs.CurrentNum == Lhs.InitialNum [[Source\Runtime\Core\Public\Containers/Array.h] [Line: 216]]
Array has changed during ranged-for iteration!

FDebug::EnsureFailed - 0.000000 s

```

---

<div class="post-metadata">

**Author:** ![KaidoomDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kaidoomdev/32/977996_2.png) [@KaidoomDev](https://forums.unrealengine.com/u/KaidoomDev)\
**Post date:** [August 12, 2022, 10:22pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/2 "2022-08-12T22:22:58Z")

</div>

This is caused by modifying an array during a for each loop for example, you don’t want to add/remove elements during the iteration. What are you trying to do exactly ? if you can post your code I can suggest an alternative way.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 12, 2022, 10:29pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/3 "2022-08-12T22:29:38Z")

</div>

Thank you for reply Sir, this is 300 lines of code, looping through all items and if found anything , adding to the array and then from array spawning the item, everything works fine.

the problem starts when I try to randomize the array before spawning items.

```auto
Item = Arr[FMath::RandRange(0, Arr.Num() - 1)];

```

`Item` is the returning array with contain items to be spawned

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 12, 2022, 10:33pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/4 "2022-08-12T22:33:01Z")

</div>

I don’t think you will read all the 300 lines of code to get the things how are they implemented…

---

<div class="post-metadata">

**Author:** ![KaidoomDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kaidoomdev/32/977996_2.png) [@KaidoomDev](https://forums.unrealengine.com/u/KaidoomDev)\
**Post date:** [August 12, 2022, 10:36pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/5 "2022-08-12T22:36:56Z")

</div>

Your issue is probably modifying the array and letting the loop continue.  
If you’re going to modify the array make sure you exit out of the loop after that using break or return.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 12, 2022, 10:41pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/6 "2022-08-12T22:41:42Z")

</div>

I found that I already have a break in this loop where I am modifying the Array if the weapon is added, remove the first row which is not a weapon and add the corresponding ammo type of that weapon.

```auto
for (auto& Array : ItemsIDArr)
	{
		if (Array.Type != EItemType::E_Weapon)
		{
			ItemsIDArr.RemoveAt(1);
			break;
		}
	}
```

---

<div class="post-metadata">

**Author:** ![KaidoomDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kaidoomdev/32/977996_2.png) [@KaidoomDev](https://forums.unrealengine.com/u/KaidoomDev)\
**Post date:** [August 12, 2022, 10:54pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/7 "2022-08-12T22:54:03Z")

</div>

What about the shuffle ?

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 12, 2022, 10:57pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/8 "2022-08-12T22:57:41Z")

</div>

shuffle I am using like this.

```auto
FCustomThunkTemplates::Array_Shuffle(Arr);

```

I don’t really know to implement it without using blueprint library… but it works fine, it randomize the items fine if the array size is `200`

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 12, 2022, 11:09pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/9 "2022-08-12T23:09:26Z")

</div>

Sir can you correct me about this bp script

![image_2022-08-13_040431245](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/b/8/7b8fbb339a4d40fb50b55ff6d3936d31ca3f93e5.png)

i have it like

```auto
if (i == -1) 
	{
		//some code
	}
else if (i != -1)
{
	//repeat some code
}

if this correct?
```

---

<div class="post-metadata">

**Author:** ![KaidoomDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kaidoomdev/32/977996_2.png) [@KaidoomDev](https://forums.unrealengine.com/u/KaidoomDev)\
**Post date:** [August 12, 2022, 11:16pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/10 "2022-08-12T23:16:15Z")

</div>

I’m just saying that shuffle also modify the array so make sure you stop the loop when you do that, similar to add/remove.

> if (i == -1)  
> {  
> //some code  
> }  
> else if (i != -1)  
> {  
> //repeat some code  
> }
> 
> if this correct?

Apart from the redundant else if, repeating some code I would probably create a dedicated function for the task, and call it when you want to execute the same code again but maybe with different parameters.

```
if (i == -1)
{

}else //i not equal -1
{
	
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 12, 2022, 11:41pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/11 "2022-08-12T23:41:28Z")

</div>

thank you so much for assisting me 🙂

---

<div class="post-metadata">

**Author:** ![Skoutyboy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/skoutyboy/32/876676_2.png) [@Skoutyboy](https://forums.unrealengine.com/u/Skoutyboy)\
**Post date:** [November 26, 2023, 9:32pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/627369/12 "2023-11-26T21:32:44Z")

</div>

You can fix this from happening by just using a normal for loop instead of a ranged for:

int n = array.Num();  
for(int i = 0; i \< n; i++)  
{

}
