OwnerController used in two different contexts

In SimpleShooter video “Refactoring PullTrigger” at minute 6:38, we made a function called “GetOwnerController and assigned it to OwnerController”.

AController* OwnerController = GetOwnerController();

However, a few lines later we assign OwnerController as a different value.

OwnerController->GetPlayerViewPoint(OUT CameraLocation, OUT CameraRotation);

The first one seems to be about getting APawn and returning it into an AController type. Are we using the OwnerController for two different things in this case since it is set = to two different things?

Full section of code is:

bool ARifleBase::GunTrace(FHitResult& BulletHit, FVector& ShotDirection)
{
	AController* OwnerController = GetOwnerController();
	if (!OwnerController) { return false; }
	FVector CameraLocation;
	FRotator CameraRotation;
	OwnerController->GetPlayerViewPoint(OUT CameraLocation, OUT CameraRotation);
	ShotDirection = -CameraRotation.Vector();

	FVector BulletEnd = CameraLocation + CameraRotation.Vector() * MaxRange;
	BulletHit;
	FCollisionQueryParams Params(NAME_None, true, OwnerController);
	Params.AddIgnoredActor(this);
	Params.AddIgnoredActor(GetOwner());
	return GetWorld()->LineTraceSingleByChannel(OUT BulletHit, CameraLocation, BulletEnd, ECollisionChannel::ECC_GameTraceChannel1, Params);

}

AController* ARifleBase::GetOwnerController()
{
	APawn* OwnerPawn = Cast<APawn>(GetOwner());
	if (!OwnerPawn) { return; }
	
	return OwnerPawn->GetController();
}

Hi,
the line

OwnerController->GetPlayerViewPoint(OUT CameraLocation, OUT CameraRotation);

does not assign a new controller. Here we call the memberfunction GetPlayerViewPoint of the controller.
Hope this answers your question.

mss10001

I think I see. So the -> just let’s us access the location and rotation of what we assigned earlier. Makes sense. Thanks mss10001.

You should have seen the -> operator a lot already. It allows us to call a function on a pointer. Might be worth revising the Building Escape section a little.

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

Privacy & Terms