OnClick Event Dynamic Button C++

Hi, I’m using the OnClick event that runs when I click my buttons. I am creating 3 bottones from my Blueprint Widget and I call them from my c ++ code with the UPROPERTY method (meta = (BindWidget)). Now what I want to achieve is to call each button from the same method to avoid creating each call event for each button. that is, dynamically. I already know that from my event I would have to use an “if” but I do not know how to get the button that is being called. Here is my code:

.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MyUserWidget.generated.h"

/**
 * 
 */

UCLASS()
class GAMEPARTY_API UMyUserWidget : public UUserWidget
{
	GENERATED_BODY()

public:

	virtual void NativeConstruct() override;

	UPROPERTY(meta = (BindWidget))
		class UButton* NewGame;

	UPROPERTY(meta = (BindWidget))
		class UButton* Options;

	UPROPERTY(meta = (BindWidget))
		class UButton* Exit;

	UFUNCTION()
		void OnMenuClick();
};

.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyUserWidget.h"
#include "UMG/Public/Components/Button.h"
#include "UMG/Public/Components/TextBlock.h"
#include "Engine.h"
#include "MyGameUserSettings.h"

void UMyUserWidget::NativeConstruct()
{
	Super::NativeConstruct();

	NewGame->OnClicked.AddDynamic(this, &UMyUserWidget::OnMenuClick);
	Options->OnClicked.AddDynamic(this, &UMyUserWidget::OnMenuClick);
	Exit->OnClicked.AddDynamic(this, &UMyUserWidget::OnMenuClick);	
}

void UMyUserWidget::OnMenuClick()
{

}

Probably the only way is to make a derived class from UButton and add a new delegate with additional parameter so you can send the button id/name/something to distinguish different buttons.

I saw that with the unity graphics engine it is very easy to do it. There should be a way to apply it to the unreal

I just found Using the same callback function for many UButtons · ben🌱ui which has some nicer alternatives than creating a derived UButton class.