I tried to implement the TotalMassOfActors function myself before looking at how you’d do it, and I came up with the following using Google:
float UOpenDoor::TotalMassOfActors() const
{
TArray<AActor*> OverlappingActors;
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
float Total = 0.f;
for (int i = 0; i < OverlappingActors.Num(); i++)
{
UPrimitiveComponent* PrimComp = OverlappingActors[i]->GetComponentByClass(UPrimitiveComponent::StaticClass());
Total += PrimComp->GetMass();
}
// for (AActor* Actor : OverlappingActors)
// {
// Total += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
// }
UE_LOG(LogTemp, Warning, TEXT("Total weight = %f"), Total);
return Total;
}
However, this attempt gave me an error message:
"a value of type “UActorComponent *” cannot be used to initialize an entity of type “UPrimitiveComponent *”
I’m guessing that the problem is the following part of the line:
OverlappingActors[i]->GetComponentByClass(UPrimitiveComponent::StaticClass());
But I can’t understand why this returns a UActorComponent and not a UPrimitiveComponent.
Also a related question - the only difference in the documentation between GetComponentByClass() and FindComponentByClass() seems to be that FindComponentByClass() is a “native version of GetComponentByClass”. But what does “native version” mean here?