In this lesson, you will learn how to design a layered input system that dynamically promotes specific control schemes based on the player’s current state.
The Concept of Input Stacking
In Unreal’s Enhanced Input system, "Priority" determines which UInputMappingContext wins when multiple contexts contain the same UInputAction. If your character is walking, you might have a "Movement" context at priority 0. When they mount a vehicle, you don’t remove the movement context; instead, you push a "Vehicle" context onto the stack with a higher priority (e.g., priority 1). The system automatically ignores the movement bindings for actions shared by both contexts, ensuring the vehicle controls take precedence.
Implementing Dynamic Priority
To manage this cleanly, we interact with the UEnhancedInputLocalPlayerSubsystem. Rather than manually removing contexts, we use the AddMappingContext function, which accepts a Priority integer.
// Inside your Character or Component
void AMyCharacter::EnterVehicle(UInputMappingContext* VehicleContext)
{
if (APlayerController* PC = Cast<APlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem =
ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{
// Adding with Priority 1 ensures this overrides any default movement (Priority 0)
Subsystem->AddMappingContext(VehicleContext, 1);
}
}
}
Why Use Layers?
Using priority layers allows you to create modular "modes" for your gameplay. By stacking contexts, you avoid "spaghetti code" where a single input function checks a giant list of if-else boolean states (e.g., if (bIsDriving) ... else if (bIsSwimming) ...). Instead, you simply define what inputs exist in that state and let the input stack handle the conflict resolution. When the player exits the state, you call RemoveMappingContext, and the system instantly falls back to the previous priority layer underneath. This pattern keeps your player controller lean and your input logic decoupled from your state logic.
Key Takeaway: Use Input Priority Layers to stack control schemes so that higher-priority contexts automatically override lower-priority inputs without requiring complex state-checking logic.