[c++]: How can i change active camera based on controller in use?

Hi guys… me again…

So my game is going to have two portions, an offline and an online portion. Each portion will have a different camera setup. Each portion also has it’s own gamemode and controller, but would share the same player pawn.

I was wondering if it’s possible to switch cameras based on which controller is currently being used, code below:

void ASmashyCharacter::PossessedBy(class AController* InController)
{
	Super::PossessedBy(InController);

	ASmashyPlayerController* OnlineController = Cast<ASmashyPlayerController>(GetController());
	ASmashyOfflineController* OfflineController = Cast<ASmashyOfflineController>(GetController());
	if (Controller != NULL)
	{
		FViewTargetTransitionParams params;
		if (Controller == OnlineController)
		{
			OfflineCamera->Activate(false);
			FollowCamera->Activate(true);
		}
		if (Controller == OfflineController)
		{
			FollowCamera->Activate(false);
			OfflineCamera->Activate(true);
		}
	}

	// [server] as soon as PlayerState is assigned, set team colors of this pawn for local player
	UpdateTeamColorsAllMIDs();
}

Breakpoints show that the correct controllers are being called, and that it’s comparing the controllers properly, and even shows that it’s reaching the Activate() for each camera depending on which controller is in use. But the camera never switches, i’ve tried this using a button input and the same comparison code and got nothing. Any ideas?

Same goes for me, except that I think you should have used Deactivate() instead of Activate(false), looks like the argument to Activate is used for resetting. Have you solved your problem?

You activate a camera by calling SetViewTarget from the active player controller and passing the desired actor with camera component. (APlayerController::SetViewTarget | Unreal Engine Documentation)

Also, with your current setup you are activating both cameras. The boolean passed to Activate is not an activate/deactivate flag (UActorComponent::Activate | Unreal Engine Documentation)
To activate a component, call Activate; to deactivate a component, call Deactivate.

Modifying your code as follows should work:

         if (Controller == OnlineController)
         {
             OfflineCamera->Deactivate();
             FollowCamera->Activate(true);
         }
         if (Controller == OfflineController)
         {
             FollowCamera->Deactivate();
             OfflineCamera->Activate(true);
         }
        Controller->SetViewTarget(this);

It might be easier to have the player controllers call SetViewTarget directly when activated, rather than performing the check.

i.e.

//in SmashyPlayerController.cpp

ASmashyPlayerController::ActivateCamera () {
   ASmashyCharacter* SmashyCharacter = Cast<ASmashyCharacter>(GetPawn());
   if (SmashyCharacter) {
       SmashyCharacter->ActivateFollowCamera();
       SetViewTarget(SmashyCharacter);

   }

}

//in SmashyOfflineController.cpp
ASmashyOfflineController::ActivateCamera () {
   ASmashyCharacter* SmashyCharacter = Cast<ASmashyCharacter>(GetPawn());
   if (SmashyCharacter) {
       SmashyCharacter->ActivateOfflineCamera();
       SetViewTarget(SmashyCharacter);

   }

}




//in SmashyCharacter.cpp
ASmashyCharacter::ActivateFollowCamera() {
    FollowCamera->Activate();
    OfflineCamera->Deactivate();

}

ASmashyCharacter::ActivateOfflineCamera() {
    FollowCamera->Deactivate();
    OfflineCamera->Activate();

}

Of course, all this assumes both ASmashyPlayerController and ASmashyOfflineController are APlayerControllers. If one or both aren’t player controllers, you’ll have to collect the active player controller and call SetViewTarget from it.