Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 8:40pm
1
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.
c0r37py
(c0r37py::)
August 21, 2022, 8:45pm
2
Can you print the size of the ArrayLocations
just after this loop ends?
1 Like
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 8:50pm
3
the engine crashed with the
error C2148 - total size of array must not exceed 0x7fffffff bytes
c0r37py
(c0r37py::)
August 21, 2022, 9:13pm
4
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
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 9:18pm
5
Thank You Sir for helping me solve this issue the trouble of +5 days, really appreciated
Emaer
(Emaer)
August 21, 2022, 9:43pm
7
Wat?
Whats the point of storing data in array when you immediately clears that array?
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 9:50pm
8
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
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 9:59pm
9
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?
Emaer
(Emaer)
August 21, 2022, 10:21pm
10
How many elements you have in ArrayLocations
after that code?
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 10:23pm
11
it has 100 elements stored in the data table of vector locations
Emaer
(Emaer)
August 21, 2022, 10:25pm
12
Are you sure? After ArrayLocations.Empty()
call?
Alexa.Ki:
the engine freez
You are causing infinite loops
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 10:31pm
14
after that arrayLocation.Empty() call , the ArrayLocation is called somewhere to store the fresh instance of the locations available in the data table
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 10:32pm
15
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
Alexa.Ki
(Alendromeda.ki)
August 21, 2022, 11:22pm
16
this is the result of the generated item function