Why are we finding the player controller from Top-Down instead of Bottom-Up?

In Mike’s example, he used GetWorld()->GetFirstPlayerController() in order to find the player controller. However, this will only work in a single player game if I’m not mistaken and also requires us to #include Engine/World.h

Before Mike walked through the solution, this is what I came up with:

// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	FVector PlayerViewPointLocation = GetOwner()->GetActorForwardVector();
	FRotator PlayerViewPointRotation = GetOwner()->GetActorRotation();

	GetOwner()->GetInstigatorController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);

	UE_LOG(LogTemp, Warning, TEXT("Location: %s"), *PlayerViewPointLocation.ToString());
	UE_LOG(LogTemp, Warning, TEXT("Rotation: %s"), *PlayerViewPointRotation.ToString());
}

But in this case, we can simply reference the owner of our Grabber component and get the Controller directly from it, allowing for the code to work for multiplayer functionality and not have to #include extra references. It seems to work in my test, is there something I’m missing that makes Top-Down referencing here better? Thank you for your time!

Nope, what you said makes sense. Although I think using the Instigator may not be correct for all situations. It would probably be better to get the controller via casting to pawn.

1 Like

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

Privacy & Terms