Hello,
SetUserFocus calls are ignored when trying to focus a Widget in NativeConstruct() in code (or OnConstruct in Blueprint). If you add any amount of delay to the call, the call will succeed, though this isn’t a good solution. Setting focus in NativeConstruct() is a common operation since that’s where screens initialize. One thing to note is that setting focus through setting the input mode always works, though this is also not a great solution since we might not always want to reset the input mode mid-stream.
Digging around the code, the issue seems to be a discrepancy in how focus is set internally. The current code in UWidget::SetUserFocus() is:
if ( ULocalPlayer* LocalPlayer = Context.GetLocalPlayer() )
{
// HACK: We use the controller Id as the local player index for focusing widgets in Slate.
int32 UserIndex = LocalPlayer->GetControllerId();
FSlateApplication::Get().SetUserFocus(UserIndex, SafeWidget);
}
Changing this to the same mechanism that Set Input Mode UI uses, the code becomes:
if ( ULocalPlayer* LocalPlayer = Context.GetLocalPlayer() )
{
LocalPlayer->GetSlateOperations().SetUserFocus( SafeWidget.ToSharedRef() );
}
With that code, everything works great.
Is this the right solution? Am I “doing it wrong” by trying to set focus this way?