Best Way To Sort A Dynamic Array?
Posted: Wed Jun 15, 2022 4:30 pm
I have a function which adds all shootable actors within a set radius around the caller into a dynamic array. So far so good. I have set a max target limit so that only so many actors can be added to the array. In order for this to work, I created two additional arrays, giving me three arrays in total. One array for storing all possible targets, another array for storing the distance between all possible targets and the caller, and finally an array for storing the final list of targets that have been organized from closest to the caller to furthest cutting off any additional targets that go beyond the max target limit.
An example, say there are 25 possible targets around the caller and the target limit is 12. In that case, the first array would have all 25 targets added to it and the second array would have all their distances to the caller added to it. I've managed to accomplish all this without issues. The problem is when it comes to populating the third and final array. The third array should only be composed of the 12 nearest targets disregarding the rest and this is where I am stuck. I created a function that is supposed to sort targets into the third array by using the data from the first two arrays but GZDoom just hard freezes whenever it is called.
Here is the sorting function:
Any ideas why it hard freezes?
EDIT:
I've changed the function a bit and it now works correctly without freezing, performance could be a bit better. Any tips on how to rewrite it to improve performance or is this the best I'm gonna get?
An example, say there are 25 possible targets around the caller and the target limit is 12. In that case, the first array would have all 25 targets added to it and the second array would have all their distances to the caller added to it. I've managed to accomplish all this without issues. The problem is when it comes to populating the third and final array. The third array should only be composed of the 12 nearest targets disregarding the rest and this is where I am stuck. I created a function that is supposed to sort targets into the third array by using the data from the first two arrays but GZDoom just hard freezes whenever it is called.
Here is the sorting function:
Code: Select all
Void SortBeamTargetsRange(Int MaxTargets)
{
Array<Double> Ranges;
Double ClosestRange;
Bool Sort;
For (Int I = 0; I < InitialTargets.Size(); I++)
{
Ranges.Push(Distance3D(InitialTargets[I]));
}
ClosestRange = -1.0;
Sort = False;
For (Int I = 0; 0 < InitialTargets.Size(); I++)
{
If (I > -1)
{
If (!Sort)
{
If (I >= InitialTargets.Size())
{
Sort = True;
I = -1;
}
Else If (ClosestRange == -1.0 || ClosestRange > Ranges[I])
{
ClosestRange = Ranges[I];
}
}
Else
{
If (ClosestRange >= Ranges[I])
{
BeamTargets.Push(InitialTargets[I]);
InitialTargets.Delete(I,1);
Ranges.Delete(I,1);
ClosestRange = -1.0;
Sort = False;
I = -1;
If (BeamTargets.Size() >= MaxTargets)
{
Return;
}
}
}
}
}
}
Any ideas why it hard freezes?
EDIT:
I've changed the function a bit and it now works correctly without freezing, performance could be a bit better. Any tips on how to rewrite it to improve performance or is this the best I'm gonna get?