hib0823
(hib0823)
January 8, 2023, 10:23am
1
These are the same functions
void APlayerC::SortActorsByDistance(TArray<AActor*>& Actors)
{
quicksort(Actors, 0, Actors.Num() - 1);
return;
}
void APlayerC::quicksort(TArray<AActor*>& Actors, int32 low, int32 high)
{
if (low >= high) return;
int32 pivot = low;
int32 i = low + 1;
int32 j = high;
while (i <= j)
{
while (AActor::GetDistanceTo(Actors[i]) <= AActor::GetDistanceTo(Actors[pivot]))
i++;
while (AActor::GetDistanceTo(Actors[j]) >= AActor::GetDistanceTo(Actors[pivot]) && j > low)
j--;
if (i > j)
{
AActor* temp = Actors[j];
Actors[j] = Actors[pivot];
Actors[pivot] = temp;
}
else
{
AActor* temp = Actors[j];
Actors[j] = Actors[i];
Actors[i] = temp;
}
quicksort(Actors, low, j - 1);
quicksort(Actors, j + 1, high);
}
}
In c++, SortActorsByDistance is calling quicksort below.
Why the input array Actors is on the output of blueprint node?
I’m pretty sure that the c++ function’s input is the array and nothing for output.
Syedhs
(Syed)
January 8, 2023, 10:50am
2
Try changing into this:-
void APlayerC::SortActorsByDistance(const TArray<AActor*>& Actors)
hib0823
(hib0823)
January 8, 2023, 11:23am
3
Thank you for your advise, but it didn’t work. Maybe I should edit or delete functions to make another way.
Syedhs
(Syed)
January 8, 2023, 4:39pm
4
What do you mean it doesn’t work. I took 5 minutes off to produce this screenshot:
The left is without const.
The only difference with yours is this a blueprint function library (not belonging to an Actor).
hib0823
(hib0823)
January 8, 2023, 6:44pm
5
I may have misunderstood what you mean changing into
void APlayerC::SortActorsByDistance(const TArray<AActor*>& Actors).
What happened is, I literally added ‘const’ in my cpp file’s SortActorsByDistance.
And as I add const on my code I get a bunch of error lines
even where they aren’t relevant with SortActorsByDistance and causes build/compile errors.
This also happens when I add the ‘const’ only in my header file’s SortActorsByDistance
UFUNCTION(BlueprintCallable)
void SortActorsByDistance(const TArray<AActor*>& Actors);
like this, and adding const in both header file and cpp file does so.
Did I do what you intended or not? I’m honestly just asking whether this is right or not, I need a lot of help to dig in c++.
Neyl_S
(Neyl_S)
January 8, 2023, 8:52pm
6
Don’t use const
for function parameter as it prevent modification of the Actors array within the function, instead add UPARAM(ref)
UFUNCTION(BlueprintCallable)
void SortActorsByDistance(UPARAM(ref) TArray<AActor*>& Actors);