Looking into Lyra - Input Mapping Contexts
How Input Mapping Contexts work and how Lyra uses them
Input comes from hardware devices, for example when the user presses a key or uses a gamepad.
An Input Mapping Context is used to map this keystroke, mouse movements or whatever from the hardware input to an Input Action. Lyra contains several Input Mapping Contexts which are used either for:
- different physical devices, such as IMC_ShooterGame_Gamepad and IMC_ShooterGame_KBM or
- for different game modes such as IMC_Default_Gamepad and IMC_ShooterGame_Gamepad
Each Input Mapping Context maps a hardware input to an action, for example in the IMC_Default_KMB pressing the left shift key triggers the IA_Ability_Dash input action:
More than one Input Mapping Context can be in use at one time, for example the IMC_Default_KBM has actions for player movement and looking, and the IMC_ShooterGame_KMB adds actions for checking the scoreboard or using additional special attacks. These additional actions from IMC_ShooterGame_KMB are only in effect when the current game mode is the ShooterGame; Unreal dynamically applies IMCs as the game mode changes.
IMCs can be used dynamically to make small changes to gameplay. For example in the gameplay ability named GA_ADS (ADS = Aim Down Sights) we see the IMC_ADS_Speed context being added to the player when the gameplay ability starts and removed when the gameplay ability ends:
This is the IMC_ADS_Speed input mapping context:
What it does is change the sensitivity of inputs when the user is looking down the sights to make the play move more slowly. It demonstrates:
- how modifiers can be added to a hardware input before it is forwarded to the input action
- the use of custom modifiers such as Lyra Aim Inversion Setting
The Lyra Aim Inversion Setting is a simple C++ class with one function:
/** Applies an inversion of axis values based on a setting in the Lyra Shared game settings */
UCLASS(NotBlueprintable, MinimalAPI, meta = (DisplayName = "Lyra Aim Inversion Setting"))
class ULyraInputModifierAimInversion : public UInputModifier
{
GENERATED_BODY()
protected:
virtual FInputActionValue ModifyRaw_Implementation(const UEnhancedPlayerInput* PlayerInput, FInputActionValue CurrentValue, float DeltaTime) override
{
ULyraLocalPlayer* LocalPlayer = LyraInputModifiersHelpers::GetLocalPlayer(PlayerInput);
if (!LocalPlayer)
{
return CurrentValue;
}
ULyraSettingsShared* Settings = LocalPlayer->GetSharedSettings();
ensure(Settings);
FVector NewValue = CurrentValue.Get<FVector>();
if (Settings->GetInvertVerticalAxis())
{
NewValue.Y *= -1.0f;
}
if (Settings->GetInvertHorizontalAxis())
{
NewValue.X *= -1.0f;
}
return NewValue;
}
};