In this lesson, you will learn how to decouple your game’s functionality from specific physical keys by using the Enhanced Input system’s abstraction layer.
The Problem with Direct Mapping
In older systems, you might have hardcoded your "Jump" command to the Spacebar. If a player wanted to use the 'A' button on a controller or rebind their keys, you had to manually rewrite your input logic. This creates a brittle connection between the hardware (a keypress) and the gameplay logic (the character jumping).
The Solution: Abstraction
Enhanced Input introduces an abstraction layer that acts as a translator. Instead of asking, "Did the user press the Spacebar?", your code asks, "Did the user trigger the 'Jump' action?"
This is achieved using two main assets:
- Input Actions: These are abstract representations of a game intent (e.g.,
IA_Jump,IA_Move). They don't care how they are triggered. - Input Mapping Contexts: This is the bridge. You define a set of rules here, saying: "If the Spacebar is pressed OR if the Gamepad Bottom Face button is pressed, trigger
IA_Jump."
Putting it into Practice
When you write your code, you no longer look for raw keys. Instead, you bind a function to the IA_Jump action. Because the input is abstracted, you can change the keys in your Input Mapping Context at any time without ever touching your Character’s C++ or Blueprint logic.
// Instead of listening for a raw key, you listen for the Action
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
// We bind our function to the abstracted action
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &AMyCharacter::Jump);
}
By decoupling the input, you gain the ability to add support for touchscreens, steering wheels, or custom keyboard layouts simply by adding new mappings to the context file, rather than modifying your core character movement code. Your character simply knows they need to "jump," and the Enhanced Input system ensures that intent is captured regardless of the device being used.
Key Takeaway: By abstracting input into "Actions," you separate the intent of a player's command from the physical device, allowing for effortless remapping and multi-controller support.