I am currently at the Iteration Through Valid Actors lesson, and I’m working on adding up masses of objects that overlap the PressurePlate collision volume we’ve set up. I have a for loop set up here to loop though each object that is contained in the TArray OverlappingActors, which is an out parameter from the GetOverlappingActors function. To make sure the loop is working properly, I log the name of any object that is currently intersecting the collision volume. I’ve attached the whole TotalMassOfActors function for context:
float UOpenDoor::TotalMassOfActors() const
{
float TotalMass = 0.f;
// Find All Overlapping Actors.
TArray<AActor*> OverlappingActors;
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
// Add Up Their Masses.
for (int i = 0; i < OverlappingActors.Num(); i++)
{
UE_LOG(LogTemp, Warning, TEXT("%s"), *OverlappingActors[i]->GetName());
}
return TotalMass;
}
This all makes sense to me. I’ve worked with for loops plenty. However, one thing that I struggle with in C++ in particular is dealing with the myriad of data types, pointers, references, etc. of objects that are crucial to get just right in order to use a function that I want. I read up on the documentation, and it looks like what I want is either the UPrimitiveComponent::GetMass or the UPrimitiveComponent::CalculateMass function.
My unsuccessful attempts at adding mass inside the for loop look something like this (these lines would be where my UE_LOG is above):
TotalMass += OverlappingActors[i]->GetMass();
or
TotalMass += OverlappingActors[i]->CalculateMass();
I would love to hear from someone who could explain the thought process behind this type of issue: knowing that my object is of type AActor*, knowing that the function I want to use on it requires
it be a UPrimitiveComponent, and how to get my object into that type. If possible here, a more-generalized answer would be most appreciated since this seems to be a reoccurring issue that I come across.