Organizing diffenrent In-Game Modes with different Input Bindings?

Hi,

I am currently working on a project, where I need to have different control modes inside the game itself. Depending on the mode the player currently is in, I need the Input to be handled differntly.

Example:

When I am in construction mode, I want the Q key, to select a Wall Piece, that I want to place.

When I am in “Control Character” Mode, I want the same key, to do something else.

What I want to know now:

Can Keys be bound to multiple Input events? And what would be a good way to manage these modes inside the player controller, without cluttering up the player controller class?

If I can bind one key to multiple input events, each of these events would always be triggered, when I press the key that is bound to them, right? Is there a way to trigger the events only, under certain conditions? In my case, depending on the current mode you are in.

The simplest way to do this would be to create separate input components for each input modes. Keep the basic one for any common inputs you might have, then override void APlayerController::BuildInputStack:


void AMyPlayerController::BuildInputStack( TArray<UInputComponent*>& InputStack )
{
	// Adds the common input component
	Super::BuildInputStack( InputStack );

	if( IsInConstructionMode() )
	{
		InputStack.Push( ConstructionInputComponent );
	}
	else if( IsInCharacterMode() )
	{
		InputStack.Push( CharacterInputComponent );
	}
}

One important flag to know about when dealing with multiple input components is the bBlockInput flag (false by default). If multiple components have an action bound to the same key, setting this to true will stop any further components in the stack from handling the action. But this is an issue if your input components are mutually exclusive.

1 Like

Hi,

this looks interesting. Is this method getting called every frame or do i have to remove my components from the stack when i switch between modes?

It’s called by the player controller on every frame, yes. It’s a normal part of the input tick, and the InputStack reference passed is cleared out on each frame so you just maintain it depending on whatever input state you need.

Thanks mate, this seems to be the cleanest solution for me :slight_smile: