I’m having trouble getting this to compile:
void SortCoverPointsByActorDistance(TArray<AActor*>& CoverPoints, AActor* TargetActor)
{
CoverPoints.Sort([&TargetActor](const AActor* LHCoverPoint, const AActor* RHCoverPoint) {
float DistanceToLHCoverPoint = FVector::DistSquared(TargetActor->GetActorLocation(), LHCoverPoint->GetActorLocation());
float DistanceToRHCoverPoint = FVector::DistSquared(TargetActor->GetActorLocation(), RHCoverPoint->GetActorLocation());
return DistanceToLHCoverPoint < DistanceToRHCoverPoint;
});
}
I keep getting this error:
|Error|C2664|‘bool USBTTaskNode_Hide::SortCoverPointsByActorDistance::<lambda_5f025693871c2d5affd9a2f93dd13701>::operator ()(const AActor *&,const AActor *&) const’: cannot convert argument 1 from ‘T’ to ‘const AActor *&’|ActionRoguelike|C:\Program Files\Epic Games\UE_4.27\Engine\Source\Runtime\Core\Public\Templates\Sorting.h|38||
Any ideas?
Sort is kinda weird as it derefences pointers.
To fix your issue, use this:
void SortCoverPointsByActorDistance(TArray<AActor*>& CoverPoints, AActor* TargetActor)
{
CoverPoints.Sort([TargetActor](const AActor& LHCoverPoint, const AActor& RHCoverPoint) {
float DistanceToLHCoverPoint = FVector::DistSquared(TargetActor->GetActorLocation(), LHCoverPoint->GetActorLocation());
float DistanceToRHCoverPoint = FVector::DistSquared(TargetActor->GetActorLocation(), RHCoverPoint->GetActorLocation());
return DistanceToLHCoverPoint < DistanceToRHCoverPoint;
});
}