I’m trying to receive input in a user widget. I looked up ListenForInputAction and internally it uses InitializeInputComponent.
The problem is, when I call InitializeInputComponent, it doesn’t create a new input component and prints:
Unable to listen to input actions without a player controller
It’s because GetOwningPlayer() returns null in this code:
void UUserWidget::InitializeInputComponent()
{
if ( APlayerController* Controller = GetOwningPlayer() )
{
InputComponent = NewObject< UInputComponent >( this, UInputSettings::GetDefaultInputComponentClass(), NAME_None, RF_Transient );
InputComponent->bBlockInput = bStopAction;
InputComponent->Priority = Priority;
Controller->PushInputComponent( InputComponent );
}
else
{
FMessageLog("PIE").Info(FText::Format(LOCTEXT("NoInputListeningWithoutPlayerController", "Unable to listen to input actions without a player controller in {0}."), FText::FromName(GetClass()->GetFName())));
}
}
Under what circumstances GetOwningPlayer() returns null? and how can I fix it?