My Line Trace Logging implementation, stop the crash

So I found a way to read the name but it crashed immediately on Play, of course. Then I remembered accessing a null does that. Fixed it by checking the result of the LineTrace…() which returns true when successful:

/// Line-trace AKA ray-cast out to reach distance
	FHitResult Hit;
	bool bDidHit = GetWorld()->LineTraceSingleByObjectType(
		OUT Hit,
		PlayerViewLocation,
		LineTraceEnd,
		FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
		TraceParameters
	);

	if (bDidHit)
	{
		// Check hit
		FString ActorName = Hit.GetActor()->GetHumanReadableName();

		UE_LOG(LogTemp, Warning, TEXT("Trace hit %s"), *ActorName);
	}


Privacy & Terms