UE5.2 Enhanced Input Add Mapping Context

I was trying to implement the enhanced input in C++, and I found that the code provided by UE documentation is not working, but this code from other sources works well.
Could somebody enlighten me, why the documentation does not work while this one works? I really appreciate your time and explanation.

How does it is not working?

  • no error
  • It’s just the UE log is not triggered which indicates the related code is not running as well.

UE 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);
            }
        }
    }

My cpp

       // my cpp
	if (ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(GetController()))
	{
		UE_LOG(LogTemp, Display, TEXT("Init: Local Player found!"));
		if (UEnhancedInputLocalPlayerSubsystem* InputSystem = LocalPlayer->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>())
		{
			if (!InputMapping.IsNull())
			{
				UE_LOG(LogTemp, Display, TEXT("Init: Adding Mapping Context!"));
				InputSystem->AddMappingContext(InputMapping.LoadSynchronous(), 0);
			}
		}
	}

Other sources: (link)

// header
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input")
class UInputMappingContext* InputMapping;

// cpp
void AInputExampleCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
    // Get the player controller
    APlayerController* PC = Cast<APlayerController>(GetController());
 
    // Get the local player subsystem
    UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer());
    // Clear out existing mapping, and add our mapping
    Subsystem->ClearAllMappings();
    Subsystem->AddMappingContext(InputMapping, 0);
}
1 Like

I’m fairly new to Unreal Engine and trying to learn through the UE documentation and some other places online has been a challenge. Sometimes the UE documentation isn’t nearly detailed enough in it’s explainations of things and sometimes it’s just wrong. In the same web page on Enhanced Input, there is this line of code:
FVector2D 2DAxisValue = Instance.GetValue().Get();
which of course does not even compile because you can’t begin an identifier with a digit.
What I’m saying is you can’t trust that everything in the UE documentation has been verified as being correct or even working at all.

1 Like

I had the same issue. Initially I did the following:

APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(PlayerController);

However, this resulted in a nullptr. And I assume that your Player variable is also of the type APlayerController and therefore will also result in a nullptr. Luckily, the APlayerController class has the function GetLocalPlayer() which casts the member variable Player of this class to the data type ULocalPlayer and therefore returns a valid result.

APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
ULocalPlayer* LocalPlayer = PlayerController->GetLocalPlayer();

Another option would be to cast the member variable Player to the data type ULocalPlayer yourself since this is a public member variable of the APlayerController class.

APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(PlayerController->Player);

Personally, I would use the GetLocalPlayer() function.