# Do/while loop is freezing the engine? Help!

**URL:** <https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, question, CPP\
**Created:** [August 21, 2022, 8:40pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213 "2022-08-21T20:40:16Z")\
**Posts on this page:** 16\
**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 21, 2022, 8:40pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/1 "2022-08-21T20:40:16Z")

</div>

HI, the loop in my code is freezing the game, how to make it optimized or something that it will play smoothly.

What the loop does?  
`check for the Transform.locations if they are dupliacted randomize them by -1.`

```auto
do
{
	Location = Available[FMath::RandRange(0, Available.Num() - 1)];
} while 
(ArrayLocations.ContainsByPredicate([Location] (const FTransform Transform)
{
	return Transform.GetLocation() == Location.GetLocation();
}));

SpawnLocation = Location;

ArrayLocations.Add(SpawnLocation);

```

the first time I run the engine using visual studio, click play and everything works fine , I stopped the game and run it again, the engine freez and I have to close it using visual studio stop button.

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [August 21, 2022, 8:45pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/2 "2022-08-21T20:45:52Z")

</div>

Can you print the size of the `ArrayLocations` just after this loop ends?

---

<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 21, 2022, 8:50pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/3 "2022-08-21T20:50:56Z")

</div>

the engine crashed with the  
`error C2148 - total size of array must not exceed 0x7fffffff bytes`

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [August 21, 2022, 9:13pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/4 "2022-08-21T21:13:53Z")

</div>

clear the `ArrayLocations` before the second time you execute the loop, right in the end where you add spawnlocation to the array, you don’t need arraylocation to store the old data.

```auto
SpawnLocation = Location;
ArrayLocations.Add(SpawnLocation);
ArrayLocation.Empty(); // this can fix the crash and freez issue

```

---

<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 21, 2022, 9:18pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/5 "2022-08-21T21:18:48Z")

</div>

Thank You Sir for helping me solve this issue the trouble of +5 days, really appreciated 🙂

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [August 21, 2022, 9:20pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/6 "2022-08-21T21:20:05Z")

</div>

glad it helps you

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [August 21, 2022, 9:43pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/7 "2022-08-21T21:43:17Z")

</div>

Wat?  
Whats the point of storing data in array when you immediately clears that array?

---

<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 21, 2022, 9:50pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/8 "2022-08-21T21:50:09Z")

</div>

storing the data in the array just to grab the locations and then recheck if the locations are duplicated.

ArrayLocations is the `reference` of `const Available`

---

<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 21, 2022, 9:59pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/9 "2022-08-21T21:59:37Z")

</div>

This is the full code,

```auto
for (auto SpawnArrayElements : ItemsIDArr)
{
	do
	{
		Location = Available[FMath::RandRange(0, Available.Num() - 1)];
	} while (ArrayLocations.ContainsByPredicate([Location](const FTransform Transform)
		{
			return Transform.GetLocation() == Location.GetLocation();
				//&& Transform.GetRotation() == Location.GetRotation()
				//&& Transform.GetScale3D() == Location.GetScale3D();
		}));
		SpawnLocation = Location;
		ArrayLocations.Add(SpawnLocation);
}
	ArrayLocations.Empty();

```

I am doing something wrong?

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [August 21, 2022, 10:21pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/10 "2022-08-21T22:21:35Z")

</div>

How many elements you have in `ArrayLocations` after that code?

---

<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 21, 2022, 10:23pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/11 "2022-08-21T22:23:15Z")

</div>

it has 100 elements stored in the data table of vector locations

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [August 21, 2022, 10:25pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/12 "2022-08-21T22:25:28Z")

</div>

Are you sure? After `ArrayLocations.Empty()` call?

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [August 21, 2022, 10:28pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/13 "2022-08-21T22:28:12Z")

</div>

> [@Alexa.Ki](#):
>
> the engine freez

You are causing infinite loops

---

<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 21, 2022, 10:31pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/14 "2022-08-21T22:31:13Z")

</div>

after that arrayLocation.Empty() call , the ArrayLocation is called somewhere to store the fresh instance of the locations available in the data table

---

<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 21, 2022, 10:32pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/15 "2022-08-21T22:32:04Z")

</div>

how is it possible, the first time I launch the game this is not infinite loop, the second time I launch the game it freezez

---

<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 21, 2022, 11:22pm UTC](https://forums.unrealengine.com/t/do-while-loop-is-freezing-the-engine-help/632213/16 "2022-08-21T23:22:45Z")

</div>

![image_2022-08-22_042150808](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/4/d/74ddc9741020977fd60e8212ef369efbff9d5024.jpeg)  
this is the result of the generated item function
