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?
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++.
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.
// 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);
}
}
}