How to focus a Common UI button (UCommonButtonBase) in Unreal Engine

As of UE 5.4, the Common UI button (UCommonButtonBase) still doesn’t support direct focus settings. This is because UCommonButtonBase is derived from UUserWidget, which supports focusing, but doesn’t direct the focus to the underlying button itself automatically. However, you can still set the focus on the button by performing a “deep” focus on the Slate button. Here’s how you can achieve this: /** * Sets the focus on the button * This function performs the "deep" focus on the Common UI button, which means that it will set the focus on the button itself * This is useful since UCommonButtonBase is derived from UUserWidget, which doesn't support focus when setting it directly */ UFUNCTION(BlueprintCallable, Category = "mod.io|UI|Button", DisplayName = "Set Button Focus (Common UI)") void SetCommonUIButtonFocus() { #if UE_VERSION_OLDER_THAN(5, 3, 0) if (bIsFocusable) #else if (IsFocusable()) #endif { if (TSharedPtr<SButton> SlateButton = GetSlateButton()) { if (SlateButton->SupportsKeyboardFocus()) { FSlateApplication::Get().SetKeyboardFocus(SlateButton, EFocusCause::Mouse); UE_LOG(LogTemp, Log, TEXT("Set focus on button '%s' (extended way)"), *GetName()); } else { UE_LOG(LogTemp, Warning, TEXT("Trying to set focus on button '%s' but the button does not support keyboard focus"), *GetName()); } } else { UE_LOG(LogTemp, Warning, TEXT("Trying to set focus on button '%s' but the slate button could not be found"), *GetName()); } } else { UE_LOG(LogTemp, Warning, TEXT("Trying to set focus on button '%s' but the button is not focusable"), *GetName()); } } /** * Gets the Slate button widget * The button is highly encapsulated and this function tries to scan the widget tree to find the button * @return The Slate button widget if found, nullptr otherwise */ TSharedPtr<SButton> GetSlateButton() const { if (WidgetTree && WidgetTree->RootWidget) { if (UButton* InternalButton = Cast<UButton>(WidgetTree->RootWidget)) { // UCommonButtonInternalBase::RebuildWidget() creates a SBox wrapper for the button if (TSharedPtr<SBox> BoxButtonWrapper = StaticCastSharedPtr<SBox>(TSharedPtr<SWidget>(InternalButton->GetCachedWidget()))) { if (BoxButtonWrapper->GetChildren() && BoxButtonWrapper->GetChildren()->Num() > 0) { if (TSharedPtr<SButton> InternalButtonSlate = StaticCastSharedPtr<SButton>(TSharedPtr<SWidget>(BoxButtonWrapper->GetChildren()->GetChildAt(0)))) { return InternalButtonSlate; } } } // UButton::RebuildWidget() returns the button directly else if (TSharedPtr<SButton> InternalButtonSlate = StaticCastSharedPtr<SButton>(InternalButton->GetCachedWidget())) { return InternalButtonSlate; } else { UE_LOG(LogTemp, Error, TEXT("Could not find the Slate button widget for button '%s'"), *GetName()); } } } return nullptr; }

August 27, 2024 · 2 min · Georgy Treshchev

How to get the currently focused widget in Unreal Engine

In Unreal Engine, you can determine the currently focused widget in both Slate and UMG UI systems. Slate To get the currently focused Slate widget, you can use the following code snippet: // Instead of 0, you can pass the user index if you have multiple users TSharedPtr<SWidget> FocusedSlateWidget = FSlateApplication::Get().GetUserFocusedWidget(0); UMG Getting the currently focused UMG widget is a bit more involved, since there is no direct function to get it or the place where it is stored. However, you can iterate over all UMG widgets and compare their cached Slate widgets to the focused one. This approach is not recommended for performance-critical code, but it can be useful for debugging or testing purposes. Here is an example function that does this: ...

March 23, 2024 · 1 min · Georgy Treshchev

How to execute code on scope exit in Unreal Engine

If you want to execute some code when the scope is exited, you can use the following handy ON_SCOPE_EXIT macro, like this: int32 AnyFunction() { ON_SCOPE_EXIT { // This code will be executed when the scope is exited (after the return statement) }; // Do smth // Just return 100 for example return 100; }

March 2, 2024 · 1 min · Georgy Treshchev

How to save and restore any value with RAII approach in Unreal Engine

Imagine you’re in a situation where you need to save and restore a value. For instance, let’s say you have a UMG widget that you want to hide temporarily, do some stuff, and then bring back its previous visibility state, all within a single function. The most straightforward approach might be to store the value in a temporary variable, modify it, and then revert it back, like this: ESlateVisibility VisibilityState; const ESlateVisibility PreviousVisibility = VisibilityState; VisibilityState = ESlateVisibility::Hidden; // Do smth. VisibilityState is Hidden here // Restore the previous visibility state VisibilityState = PreviousVisibility; This method gets the job done, but it’s not very scalable. What if you need to manage multiple values? What if there are several exit points in your function? What if multiple functions require saving and restoring the same value? Duplicating this rather verbose code everywhere is not ideal. We can do it better. ...

February 25, 2024 · 2 min · Georgy Treshchev

How to cast Slate widgets in Unreal Engine

This brief article addresses the question of how to cast one Slate widget to another in Unreal Engine. Imagine you have an SWidget widget wrapped in a shared pointer like this: TSharedPtr<SWidget> MyWidgetPtr; If you want to cast it, for example, to an SSpacer, your code might look like this: TSharedPtr<SSpacer> MySpacerPtr = StaticCastSharedPtr<SSpacer>(MyWidgetPtr); It’s important to keep in mind that you should verify whether the shared pointer is valid before using it. The same approach works for shared references: ...

October 21, 2023 · 1 min · Georgy Treshchev