Unreal Engine 5 · Blueprint → C++
Bind Button OnClicked in Unreal Engine 5 C++UE Docs
Binding a Button's OnClicked event in C++ uses PlayButton->OnClicked.AddDynamic(this, &UMyWidget::OnPlayClicked), where the handler is a UFUNCTION()-marked method bound in NativeConstruct.
Blueprint node & C++ equivalent
UFUNCTION()
void OnPlayClicked();PlayButton->OnClicked.AddDynamic(this, &UMyWidget::OnPlayClicked);What does binding a Button OnClicked event do?
In Blueprint you drag off a Button's OnClicked pin to run nodes when the player clicks it. In C++ you bind a function to the button's OnClicked multicast delegate so your method runs on each click.
The bound function must be a UFUNCTION() so the dynamic delegate system can reflect and call it.
The C++ equivalent
Declare the handler in the header with the UFUNCTION() macro, for example UFUNCTION() void OnPlayClicked();. Then bind it inside NativeConstruct with PlayButton->OnClicked.AddDynamic(this, &UMyWidget::OnPlayClicked).
The button itself is usually a UPROPERTY(meta=(BindWidget)) UButton* PlayButton, which automatically links to a button of the same name in the widget Blueprint. Call Super::NativeConstruct() before binding.
Common mistakes
Omitting the UFUNCTION() macro on the handler causes a binding failure at runtime, because AddDynamic requires a reflected function. The handler must also take no parameters, since OnClicked is a parameterless delegate.
Binding in the constructor instead of NativeConstruct can fail because the BindWidget pointer may not be set yet. Bind after the widget tree is constructed.
Frequently asked questions
How do you bind a button click in UE5 C++?+
Add a UFUNCTION() handler in the header, then in NativeConstruct call PlayButton->OnClicked.AddDynamic(this, &UMyWidget::OnPlayClicked). The button is usually a BindWidget UButton property.
Why does AddDynamic fail to bind my button handler?+
The handler must be marked with UFUNCTION() and take no parameters. Without the macro, the dynamic delegate cannot reflect the function and the bind fails.
Where should I bind UMG button events in C++?+
Bind them in NativeConstruct after calling Super::NativeConstruct(). At that point BindWidget pointers like PlayButton are valid.