Course contentsShow
← Unreal Engine Game Development
Lesson 241 of 8804. Advanced Input and PhysicsFree lesson

Integrating Input Consumption for UI Interactivity

You will manage input consumption settings to ensure that game world controls are correctly suppressed when players interact with complex user interfaces.

In this lesson, you will learn how to configure your input system so that gameplay actions stop firing the moment your player interacts with a UI menu.

When you are navigating an inventory screen or typing in a text box, you don’t want the "Jump" or "Fire" actions—which are bound to your character—to trigger simultaneously. In Unreal’s Enhanced Input system, we manage this by explicitly defining how much of an input event the UI "consumes."

The Concept of Input Consumption

By default, your character’s Input Mapping Context remains active even when a widget is visible. To prevent this, we utilize the Input Consumption settings found within our widgets and the EnhancedInputSubsystem.

When you create a UI widget, you can set the Visibility property of your root widget to Visible or SelfHitTestInvisible. This tells the slate rendering system to intercept mouse clicks and keyboard focus. However, to ensure the game doesn't also receive those inputs, we must manage our Mapping Contexts dynamically.

Implementation Strategy

The most robust way to handle this is to remove or disable your gameplay-related Input Mapping Context the moment your UI gains focus. You can handle this inside your Widget’s NativeConstruct or via a custom event:

void UMyInventoryWidget::NativeConstruct()
{
    Super::NativeConstruct();

    // Get the player controller's input subsystem
    if (auto* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetOwningPlayer()->GetLocalPlayer()))
    {
        // Remove gameplay context so character movement stops
        Subsystem->RemoveMappingContext(GameplayMappingContext);
        
        // Add a UI-specific context for menu navigation
        Subsystem->AddMappingContext(UIMappingContext, 1);
    }
}

By removing the gameplay context, you effectively "consume" the input at the UI layer. When the widget is closed, you simply reverse the process: remove the UIMappingContext and add the GameplayMappingContext back. This ensures a clean separation between your avatar’s world interactions and your menu navigation, preventing "ghost inputs" where a character might accidentally swing a sword while the player is just trying to equip an item.

Key Takeaway: To prevent gameplay actions from overlapping with UI, dynamically add and remove your Input Mapping Contexts as your menus transition between open and closed states.