GetPlayerWorldPosition vs. GetOwner()->GetActorLocation()?

As seen below, I just used GetOwner()->GetActorLocation() instead of creating a function. Why would I want to build a function?

Thank you!

	FVector LineTraceEnd = GetLineTraceEnd();

	FHitResult Hit;
	// Ray-cast out to a certain distance (reach)
	FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
	GetWorld()->LineTraceSingleByObjectType
	(
		OUT Hit,
		GetOwner()->GetActorLocation(),
		LineTraceEnd,
		FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
		TraceParams
	);
		return Hit;

If you make it into a function, you can have it perform additional tasks with a single call. It’s a bit faster that way. Though if you just need that 1 thing, what you’re doing is fine and there’s no need to make a function.

In the code you gave though, specifically what you may need is the player’s viewpoint, and GetPlayerWorldPosition() gives you both your position and rotation, so if you have that outputting to member variables, you can just call the function at the top of whatever other function uses it, then use the newly-updated member variable. that way you have the rotation too if you need it (which you might).

Makes sense! Thank you ShadowGamer3! :smiley:

Correction: its not GetPlayerWorldPosition()

this is what i meant:

GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
	OUT CameraLocation,
	OUT CameraRotation
);

this will give you the player’s position & rotation (viewpoint)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms