Which C++ base class would you use to implement 'on key press' character possession?


I have a couple of characters on my map and I want to be able to switch possession between them.
Lets say, if you press ‘1’ you possess character 1 and if you press ‘2’ you possess character 2.

I implemented it quickly in the level BP as follows:

Now I want to implement the same in C++, as a PossessionController class.
Since I want to listen to keyboard events, I thought inheriting the Pawn’s SetupPlayerInputComponent(UInputComponent PlayerInputComponent) method will come in handy.
However, I’m sure there are cleaner ways of doing that.

How would you implement such a controller?

One way to implement in C++ is using custom Player Control APlayerController class. Call this class from Game Mode class, you can also call your Game Mode class to be set as default C++ only or call it from blueprint by setting UCLASS(Blueprintable) and extend its implementations. One way to think about Game Mode C++ class is as a container for all the main component managers like ( APlayerController class, DefaultPawnClass)which will be called initially according to UE4 pipeline. You can use your custom player controller class for many purposes like controlling players (RTS, multiplayer etc) and handle it within the class, saving the call, as in blueprints every time to call player controller.

Let’s assume that we have two classes AMyCharacterClass and APlayerControllerManager , where character class calls functions in controller class and not the other way. Below is a simple example:

In Custom Player Controller .h:

class YourProject_API APlayerControllerManager : public APlayerController

	// Character class pointer where characters have their behaviors. Used to call to store result of character object from casting dynamically.
	class AMyCharacterClass* myCharacterController;

    //Cal this method in your character class to change character. Call casting here for the player.
    void ChangePlayerController(AMyCharacterClass* characterObj);

        //Setup Input Components- keybindings etc.
    	virtual void SetupInputComponent() override;        

In Custom Player Controller .cpp :

void APlayerControllerManager::ChangePlayerController(AMyCharacterClass* characterObj)

// Called to bind functionality to input

  void AJumpScript::SetupPlayerInputComponent()
  //Get character object using cast of that class.
    myCharacterController = Cast< class AMyCharacterClass >(UGameplayStatics::GetPlayerCharacter(GetWorld(), NULL));
 //Bind Actions for keys as created in project settings, pas character object and call possess function in character class.
   	InputComponent->BindAction("FirstPlayer", IE_Pressed, myCharacterController, &AMyCharacterClass::PossessFirstPlayer);
   	InputComponent->BindAction("SecondPlayer", IE_Released, myCharacterController, &AMyCharacterClass::PossessSecondPlayer);

In MyCharacterClass.cpp, create the two functions PossessFirstPlayer() and PossessSecondPlayer() and call the ChangePlayerController () function using PlayerControllerManager 's object in them. One way is :

void AMyCharacterClass::PossessFirstPlayer()
  //Create Csstom Player Controller object and cast from the class.
    APlayerControllerManager* myControllerObj= Cast< class APlayerControllerManager >(UGameplayStatics::GetPlayerController(GetWorld(), NULL));

//pass this character's object to custom controller to possess when the key is pressed
  myControllerObj-> ChangePlayerController(this);


I hope it helps!

Thank you.
Seems like APlayerController is indeed the right tool for the job.