Download

How to handle input for multiple actors?

I am trying to make a prototype for new game
My game has 3 different characters, they are all placed in the world and you can switch between them by pressing tab

I am not sure how will i handle the input.

  • I can create 3 player controllers, each per character and implementing the tab logic in each one of them
  • I can create 1 player controller which pass the input to the selected character

The first solution is ok but i will have code duplication at each one of the player controller of the switching characters logic
The second solution solves it but it sounds like it is not the way i am supposed to to this

What is the best way here? it there a third way that i am missing?

Perhaps implement the input directly on the characters?

How is that different from the first solution?
sorry didnt quite understood

Hi!

I can create 1 player controller which pass the input to the selected character.

This is exactly what the docs says:

The PlayerController would still have to switch control between characters but each character could have different input if necessary. Shared input actions could be inherited from a character base class.
Having 3 PlayerControllers would not be needed as you would simple possess one character at a time with the same PlayerController.

Great thanks guys!

Shipped 3 projects where player can control multiple characters. Approach was similar to what GarnerP57 outlined:

  • 1 player controller where all input is setup and processed
  • player controller passes input to whatever character it currently possesses
  • player controller handles common inputs like ESC or F1 to bring up menus (otherwise it would be duped in all characters)
  • common interface for all player actions that both player controller and characters implement (MoveLeft(), MoveForward(). etc.)

The player controller is “the will of the player” and characters carry out the will of the player in different ways.

Isn’t that what the possession system is for? The whole point of the controller -> pawn relationship is to have a simple system for switching what pawn is controlled by a given player/ai controller. Just call PlayerController::Posses(TargetPawn).