During Building Escape - 126 - Which deals with refactoring our Grabber files, we are presented with abstracting LineTraceEnd and it’s components into a new function.
Later on, we see that this causes a conflict because we also need PlayerViewPointLocation. The instructor chooses to make another function that returns this value.
I paused the video early on to see if I could get through the refactor without guidance. This resulted in a different approach to the PlayerViewPointLocation issue.
After doing a bit of Googling, I decided to have GetPlayersReach return an array containing both our LineTraceEnd and our PlayerViewPointLocation. Here is the updated function:
FVector * UGrabber::GetPlayersReachAndLocation() const
{
// Get players viewpoint
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
// Creating an array so that we can return Reach and Location at once.
static FVector VectorArray[2];
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation);
VectorArray[0] = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
VectorArray[1] = PlayerViewPointLocation;
return VectorArray;
}
When I end up needing either value it looks something like this:
DrawDebugLine(
GetWorld(),
//Location
GetPlayersReachAndLocation()[1],
//Reach
GetPlayersReachAndLocation()[0],
FColor(255, 255, 255),
false,
0.f,
0,
10.f
);
Is this bad practice? Am I opening myself up for any performance issues? What’s the best way to answer these questions. I’m a bit weary of going with something just because it works.