Do/while loop is freezing the engine? Help!

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.

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.

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

1 Like

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

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.

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

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

glad it helps you

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

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

This is the full code,

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?

How many elements you have in ArrayLocations after that code?

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

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

You are causing infinite loops

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

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


this is the result of the generated item function