Setting the active camera on an APlayerController

Hi! I have a somewhat modified version of UE4.18, specifically for a stop-motion tactical RTS game, without networking; I’m having trouble seeing where and when a camera/camera component is set as active for a given APlayerController. Take a look at the following Blueprint class, derived from an almost-empty class derived from my modified APlayerController. I have not seen any Camera actor or UCameraComponent inside APlayerController’s data members;

The selected ActiveCamera is an UCameraComponent I have added in the C++ class. I am interested in the camera that is in front of the selected camera: where can I find that data member and how do I activate/deactivate it at runtime?

My guess is that it’s somehow related to APlayerController::PlayerCameraManager, but I have not been able to see how. Any help would be appreciated!

The way cameras work in UE4 is that one actor (any actor, it can be even PlayerController and GameMode if you wish) is assigned as so called View Target and that actor on every frame is asked about how it wants to displayed by requesting from actor a camera position, by calling CalcCamera() in them. By default on pawn possession the possessed actor also becomes view target but you can disable that. Default CalcCamera code in AActor finds first active camera component and positions camera based on camera component locations.

In blueprint only situation you need activate and deactivate camera components, but since you in C++ you can override CalcCamera function, which i explaned here:

here you have default ClacCamera code:

void AActor::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult)
{
	if (bFindCameraComponentWhenViewTarget)
	{
		// Look for the first active camera component and use that for the view
		TInlineComponentArray<UCameraComponent*> Cameras;
		GetComponents<UCameraComponent>(/*out*/ Cameras);

		for (UCameraComponent* CameraComponent : Cameras)
		{
			if (CameraComponent->bIsActive)
			{
				CameraComponent->GetCameraView(DeltaTime, OutResult);
				return;
			}
		}
	}

	GetActorEyesViewPoint(OutResult.Location, OutResult.Rotation);
}

You can improve performerce by directly pointing to right camera component insted of searching for it as default code does, you also don’t need to follow camera components at all if you wish you can directly compute camera position here. You set camera postioning by changing values in OutResult. Remeber that same as Tick CalcCamera is called on every frame, so you should not do run any heavy code here or it will hit fps

For those looking this up, here’s exactly what you have to do: On the controller, set bAutoManageActiveCameraTarget to false, add a new UCameraComponent, call NewCameraComponent->SetActive(true), call SetViewTarget(this) in order to get this controller’s CalcCamera() method called, and override CalcCamera(…) to provide the location and rotation of NewCameraComponent. The elegant way to do this would be to use a CameraManager component (not the APlayerCameraManager) to compute/store and retrieve the data you need through Owner’s CalcCamera() method.

1 Like

I don’t think there exists a CameraManager component :slight_smile: