Use a bool instead of a pointer for if statement?

I’ve used a bool, GotHit, returned from LineTraceSingleByObjectType() in the if statement that logs the hit actor name to the console. As in…

/// Ray-cast out to reach distance
FHitResult Hit;
bool GotHit = GetWorld()->LineTraceSingleByObjectType(
OUT Hit,
PlayerViewPointLocation,
LineTraceEnd,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters
);

/// See what we hit
if(GotHit)
{
AActor *Actor = Hit.GetActor();
UE_LOG(LogTemp, Warning, TEXT(“Got a hit on %s!”), *Actor->GetName());
}

This makes more sense to me as your testing a bool instead of a pointer and it will always be either true or false and never null or nullptr. And, you don’t create another pointer just to perform the test, making the code more efficient. I know it’s very minimal but it seems in gaming every clock cycle counts. :slight_smile:

Does my thinking make sense here?

1 Like

Privacy & Terms