UserWidget Class in Unreal Engine 5 (C++)

The UUserWidget class is the base class for all UI elements in Unreal Engine that use UMG (Unreal Motion Graphics). It provides the necessary functionality to create, manage, and update UI elements within a game.

Below is a structured breakdown of the UUserWidget class in terms of public, private, and protected members, functions, and delegates.


UObject └── UGCObject └── UInterface └── UVisual └── UWidget └── UPanelSlot └── UContentWidget └── UUserWidget

Hierarchy of UUserWidget in Unreal Engine 5

The UUserWidget class is part of the UMG (Unreal Motion Graphics) system in Unreal Engine. It extends multiple base classes to provide widget functionality, including ticking, event handling, and user interaction. Below is a breakdown of the complete class hierarchy from the topmost base class down to UUserWidget.


Hierarchy Structure:

UObject
 └── UGCObject
      └── UInterface
 └── UVisual
      └── UWidget
           └── UPanelSlot
           └── UContentWidget
           └── UUserWidget

Class Descriptions:

Class Parent Class Purpose
UObject None The base class for all UE objects. Provides memory management, serialization, and garbage collection.
UGCObject UObject An intermediate class for garbage collection management.
UInterface UGCObject Used for defining Unreal interfaces that classes can implement.
UVisual UObject Abstract base class for all visual UI elements. It does not contain any rendering logic.
UWidget UVisual The base class for all UMG widgets (buttons, images, sliders, etc.). Provides functionality for rendering and interaction.
UPanelSlot UWidget Represents the slot that a widget occupies inside a panel (like a grid or canvas slot).
UContentWidget UWidget A specialized UWidget that contains a single child widget (e.g., UButton).
UUserWidget UWidget The main class for user-created UI. Allows Blueprint or C++ customization of UI behavior.

Detailed Breakdown of Important Classes

1. UObject

  • The root of all Unreal classes.
  • Provides reflection, serialization, and garbage collection.
  • Used as a base for all engine and game objects.

2. UGCObject

  • A lightweight UObject alternative used for efficient garbage collection.
  • Used in UI and gameplay classes that don’t need full UObject functionality.

3. UInterface

  • Allows classes to implement interface functionality.
  • Interfaces in Unreal are used to create reusable behavior without enforcing class inheritance.

4. UVisual

  • The base class for all UI elements.
  • Provides an abstract representation of something that can be drawn on the screen.

5. UWidget

  • The core base class for all UI elements in Unreal Engine.
  • Defines behavior for UI elements such as buttons, text, sliders, and images.
  • Provides properties like:
    • Visibility – controls if the widget is shown or hidden.
    • RenderTransform – allows rotation, scale, and movement of UI elements.
    • IsEnabled() – determines if the widget can interact with input.
  • Every interactive UI element inherits from UWidget.

6. UPanelSlot

  • Represents a slot inside a panel.
  • Used to define the layout properties of widgets within parent panels.
  • Example: A UButton inside a UVerticalBox uses a UVerticalBoxSlot (which inherits from UPanelSlot).

7. UContentWidget

  • A specialized UWidget that only contains a single child widget.
  • Example: A UButton can have one child widget (like text or an image).

8. UUserWidget

  • The main user interface class for game developers.
  • Can be extended using Blueprints or C++ to create custom HUDs and menus.
  • Contains features like:
    • Widget Tree (UWidgetTree* WidgetTree) – Holds all child widgets.
    • Animations – Supports UI animations with UWidgetAnimation.
    • Input Handling – Can handle keyboard/mouse events.
    • Event Lifecycle – Functions like NativeConstruct() and NativeTick().

Conclusion

  • UUserWidget is built on top of UWidget, which itself derives from UVisual and UObject.
  • UUserWidget enables the creation of interactive UI elements, HUDs, menus, and more.
  • Lower-level classes like UWidget provide essential rendering and interaction functionality.
  • UVisual is a foundation class that ensures all UI elements have a visual representation.

Would you like a code example on how to create a UUserWidget in C++? 🚀

Breakdown of UUserWidget Class

Category Members / Functions Purpose
Public Members UWidgetTree* WidgetTree Stores the hierarchy of widgets inside this UserWidget.
Private Members TWeakObjectPtr<UWorld> World Weak reference to the world context, preventing memory leaks.
  bool bIsDesignTime Indicates if the widget is in the UMG designer.
  FName TickSpace Stores the ticking behavior of the widget.
Protected Members bool bInitialized Tracks whether the widget has been initialized.
  bool bHasScriptImplementedTick Checks if the widget has a Blueprint-implemented tick function.
  bool bHasScriptImplementedPaint Checks if the widget has a Blueprint-implemented paint function.
  TWeakObjectPtr<APlayerController> OwningPlayer Stores the Player Controller that owns this widget.
Public Functions void AddToViewport(int32 ZOrder = 0) Adds the widget to the viewport.
  void RemoveFromParent() Removes the widget from its parent.
  APlayerController* GetOwningPlayer() const Retrieves the player controller that owns this widget.
  UWorld* GetWorld() const override Returns the world context for this widget.
  void SetVisibility(ESlateVisibility InVisibility) Sets the widget’s visibility state.
  void PlayAnimation(UWidgetAnimation* InAnimation, float StartAtTime = 0.0f, int32 NumLoopsToPlay = 1, EUMGSequencePlayMode::Type PlayMode = EUMGSequencePlayMode::Forward, float PlaybackSpeed = 1.0f) Plays an animation on this widget.
Private Functions void Initialize() Initializes the widget.
  void OnInitialized() Internal function called after initialization.
  void Tick(float DeltaTime) Handles per-frame updates.
  void HandleVisibilityChanged() Handles visibility changes.
Protected Functions virtual void NativeConstruct() Called when the widget is constructed (like BeginPlay for widgets).
  virtual void NativeDestruct() Called when the widget is being destroyed.
  virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) Called every frame if ticking is enabled.
  virtual bool NativeOnKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent) Handles key press events.
  virtual bool NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent) Handles mouse button press events.
Delegates FOnVisibilityChanged OnVisibilityChanged Delegate triggered when the widget’s visibility changes.
  FOnAnimationFinished OnAnimationFinished Delegate triggered when an animation finishes playing.

Summary

  • Public Members store the widget tree and general properties accessible from outside the class.
  • Private Members manage internal widget states and references (e.g., world, design-time status).
  • Protected Members are used for subclass management, such as checking if the widget has been initialized.
  • Public Functions allow developers to add/remove widgets, play animations, or modify visibility.
  • Private Functions handle internal logic like ticking, initialization, and visibility changes.
  • Protected Functions provide hooks for extending widget behavior when subclassing UUserWidget.
  • Delegates notify when animations or visibility changes occur, useful for event-driven UI logic.

Would you like a usage example in a C++ class? 🚀