In this lesson, you will learn how to bridge your User Interface (UI) with powerful engine commands by binding button clicks in your Widget Blueprint to custom C++ logic.
The Bridge Between UI and Engine Logic
When building tools or complex game menus, you often need the user to trigger technical operations—like spawning specific actors, clearing temporary cache files, or toggling rendering features—directly from a button press. Since these operations usually live in C++, you need a way for your Widget Blueprint to "call" that code.
The pattern works like this:
- Define the C++ Function: Create a function in your C++ class (often inside an Editor Utility Widget or a custom Actor Component) that executes the desired engine command. Mark it as
UFUNCTION(BlueprintCallable). - Expose to UI: Because you marked the function as
BlueprintCallable, it becomes available in the Event Graph of your Widget Blueprint. - Bind the Event: In your Widget's "Designer" tab, select your button. In the "Details" panel, scroll to the "Events" section and click the green "+" button next to
OnClicked. - Trigger the Command: Inside the resulting event node, drag out a wire and search for your C++ function name. Connect the nodes.
Concrete Example
Imagine you want a button that forces a garbage collection cycle to test memory management.
C++ Header:
UFUNCTION(BlueprintCallable, Category = "DevTools")
void TriggerGarbageCollection();
C++ Source:
void UMyWidgetLogic::TriggerGarbageCollection()
{
GEngine->ForceGarbageCollection(true);
}
Once compiled, you simply drag the TriggerGarbageCollection node from the palette into your Widget’s Event Graph and plug it into the OnClicked execution pin. Now, every time the player or editor user clicks that button, the engine executes the command instantly.
This workflow allows you to keep your heavy, engine-level commands safely written in optimized C++, while providing a clean, accessible interface for designers or players to trigger them via your custom UI.
Key Takeaway: By marking C++ functions as BlueprintCallable, you create a direct communication channel that allows UI widgets to execute complex engine operations with a single button press.