Help: How can I switch the function bound to the 'E' key in Enhanced Input?

Input->BindAction(IA_E, ETriggerEvent::Triggered, this, &APeopPlayController::EKey)

If the ‘E’ key is bound to this function elsewhere, but I want it to bind to another function when the player enters a specific area, how should this be done?

How can I switch to the EKey2 function, for example?

output
Hi @StevenX,

I’d recommend using a second Input Mapping Context with higher priority when the player enters that area.

Here I have the player spawn cubes with the E key, but it switches to spawning spheres when it enters a specific collision area. I’ve done this example with Blueprints, but it’ll work just as well with C++.


I have two Input Mapping Contexts setup


The default one has all my regular mappings, including the input action for cube spawn on the E key.


My second one only has the input I’d like to override in the special zone. It is also set to the E key.


When the character detects that its entered the collision area - the “SphereZone” - it adds the second mapping context at a higher priority, so that the E key will trigger it instead of the regular cube spawn function.

Here’s the full blueprint code:


For C++, Epic has some examples of how to do it in the Unreal Documentation:

// Expose a mapping context as a property in your header file...
UPROPERTY(EditAnywhere, Category="Input")
TSoftObjectPtr<UInputMappingContext> InputMapping;
	 
// In your cpp...
if (ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(Player))
{
	if (UEnhancedInputLocalPlayerSubsystem* InputSystem = LocalPlayer->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>())
	{
		if (!InputMapping.IsNull())
		{
			InputSystem->AddMappingContext(InputMapping.LoadSynchronous(), Priority);
		}
	}
}

Hope this helps!

Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.