Thank you very much for the quick response!
Yes, this way it works, thanks again for your help
And also for showing how to get the StaticClass from a class.
Also I have managed to implement it more or less correctly.
CalculateMassOfComponents is static in the declaration. Not a fan of double traversing, but our DefaultPawn has 2 components, for example. Is doing that with an Iterator a better approach than with higher order functions?
float UDoorOpen::TotalMassOfActors() const
{
// Find all overlapping actors - is it possible to move this logic to store the actors only once?
TArray<AActor*> OverlappingActors;
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
UE_LOG(LogTemp, Warning, TEXT("Number of overlapping actors: %i"), OverlappingActors.Num());
// Add masses
float TotalMass = Algo::Accumulate(
OverlappingActors,
0.f,
[](float acc, AActor* el)
{
if (el)
{
TArray<UPrimitiveComponent*> ActorComponents;
el->GetComponents<UPrimitiveComponent>(OUT ActorComponents, false);
UE_LOG(LogTemp, Warning, TEXT("Number of overlapping components: %i"), ActorComponents.Num());
// Unless the function is static, 'this' needs
// to be captured by the lambda
return acc + CalculateMassOfComponents(ActorComponents);
}
else
return acc;
});
UE_LOG(LogTemp, Warning, TEXT("Calculated mass for overlapping components: %f"), TotalMass);
return TotalMass;
}
float UDoorOpen::CalculateMassOfComponents(const TArray<UPrimitiveComponent*> ActorComponents)
{
if (ActorComponents.Num() == 0)
return 0.f;
if (ActorComponents.Num() == 1)
// Using CalculateMass instead of GetMass
// because it is a bit faster
// If needed to get the precise mass, GetMass is better
return ActorComponents[0]->CalculateMass();
return Algo::Accumulate(
ActorComponents,
0.f,
[](float acc, UPrimitiveComponent* el)
{
return acc + el->CalculateMass();
}
);
}