# OnClick Event Dynamic Button C++

**URL:** <https://forums.unrealengine.com/t/onclick-event-dynamic-button-c/449795>\
**Category:** UI\
**Tags:** button, onclick, unreal-engine, Widget, CPP, Blueprint, question\
**Created:** [May 29, 2019, 2:19pm UTC](https://forums.unrealengine.com/t/onclick-event-dynamic-button-c/449795 "2019-05-29T14:19:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielf94](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@danielf94](https://forums.unrealengine.com/u/danielf94)\
**Post date:** [May 29, 2019, 2:19pm UTC](https://forums.unrealengine.com/t/onclick-event-dynamic-button-c/449795/1 "2019-05-29T14:19:01Z")

</div>

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()
{

}
```

---

<div class="post-metadata">

**Author:** ![NorlinGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/norlingames/32/495614_2.png) [@NorlinGames](https://forums.unrealengine.com/u/NorlinGames)\
**Post date:** [May 29, 2019, 8:51pm UTC](https://forums.unrealengine.com/t/onclick-event-dynamic-button-c/449795/2 "2019-05-29T20:51:15Z")

</div>

Probably the only way is [to make a derived class from `UButton` and add a new delegate](https://answers.unrealengine.com/questions/276531/bind-onclicked-events-to-buttons-created-at-runtim.html) with additional parameter so you can send the button id/name/something to distinguish different buttons.

---

<div class="post-metadata">

**Author:** ![danielf94](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@danielf94](https://forums.unrealengine.com/u/danielf94)\
**Post date:** [June 21, 2019, 3:15pm UTC](https://forums.unrealengine.com/t/onclick-event-dynamic-button-c/449795/3 "2019-06-21T15:15:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leroyyyy231](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Leroyyyy231](https://forums.unrealengine.com/u/Leroyyyy231)\
**Post date:** [September 3, 2022, 1:05am UTC](https://forums.unrealengine.com/t/onclick-event-dynamic-button-c/449795/4 "2022-09-03T01:05:09Z")

</div>

I just found [Using the same callback function for many UButtons · ben🌱ui](https://benui.ca/unreal/using-same-function-for-many-ubuttons/) which has some nicer alternatives than creating a derived UButton class.
