# Bind OnClicked Events to buttons created at runtime.

**URL:** <https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041>\
**Category:** UI\
**Tags:** button, unreal-engine, onclicked, runtime, UE4, question, CPP\
**Created:** [August 8, 2015, 12:33pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041 "2015-08-08T12:33:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 8, 2015, 12:33pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/1 "2015-08-08T12:33:58Z")

</div>

Hello Everyone ,

I have scrollbox with buttons created at runtime based on number of .sav files in game folder.

```
UButton *save_button = NewObject<UButton>(this);
loadgame_scrollbox->AddChild(save_button);

```

Each button represents one save file, that can be loaded. I’m trying to bind events to that buttons at runtime:

```
button_interface->OnClicked.AddDynamic(this, &UMasterUI_Menu::OnClick_button_load_game);

```

This code works, but it’s bad idea to repeat it 100 times with 100 functions (one per button). And I don’t know how many buttons will spawn, it’s based on number of .sav files. I don’t know which button called that function so I don’t know which save file should I load!

Is it possible to return Name/Index of the clicked button with OnClicked? And then use that index in OnClick\_button\_load\_game(int32 index) function to determine which button was clicked. For example OnValueChanged Event returns float Value by default(USlider) and it’s easy to access Value this way.

I need c++ solution, not BP (i’m using c++ only).

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 8, 2015, 3:42pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/2 "2015-08-08T15:42:20Z")

</div>

Hello XadE,

Well what you could try, even if it’s not beautiful, you can wrap a level of indirection on your OnClicked delegate. What I mean by that, you define a new type of delegate that return a value. this new type will do an internal invoke to your another delegate that will call your function load\_save.

With that level of indirection you’ll be able to catch the actor the Onclicked came from.

Do you follow me?

gamer08

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 8, 2015, 4:24pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/3 "2015-08-08T16:24:46Z")

</div>

Sounds good, could you explain how to do that in code?

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 8, 2015, 8:18pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/4 "2015-08-08T20:18:26Z")

</div>

Hello again XadE, I made an example for you to understand.

Header file

```
#pragma once

#include "GameFramework/Volume.h"
#include "GameFramework/Actor.h"
#include "DemoDelegateVolume.generated.h"

DECLARE_DELEGATE_OneParam(HelloDelegate, FString);

UCLASS()
class FPS4_8_API ADemoDelegateVolume : public AVolume
{
	GENERATED_BODY()
	
public:
	ADemoDelegateVolume();
	
	UFUNCTION()
	void OnBeginOverlap(AActor* overlappingActor);

	void HelloFromCharacter(FString characterNmae);

	HelloDelegate hello;
};

```

Source file

```
    #include "FPS4_8.h"
    #include "DemoDelegateVolume.h"
    #include "Engine.h"
    #include "FPS4_8Character.h"
    
    ADemoDelegateVolume::ADemoDelegateVolume()
    {
    	
    	OnActorBeginOverlap.AddDynamic(this, &ADemoDelegateVolume::OnBeginOverlap);
    	
    	//Bind function
    	hello.BindUObject(this, &ADemoDelegateVolume::HelloFromCharacter);
    }
    
    void ADemoDelegateVolume::OnBeginOverlap(AActor* overlappingActor)
    {
    	AFPS4_8Character* character = Cast<AFPS4_8Character>(overlappingActor);
    	if (character)
    	{
    		hello.Execute(overlappingActor->GetName());
    	}
    }
    
    void ADemoDelegateVolume::HelloFromCharacter(FString characterName)
    {
    	if (GEngine)
    		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString("Hello from character : ").Append(characterName));
    
    }

```

Now some details.

I did based on a volume.

I defined a new BeginOverlap that I replace the default one with. (maybe not required in your case).

I define a delegate with one parameter in my case a FName.

I’m a delegate instance on my volume to use it.

In my volume constructor I replace the OnOverlap function with my own with the addDynamic macro.

After that I bind the delegate instance to the function with 1 parameter I want ( in your case it will be OnClick\_button\_load\_game with the parameter you want)

After on my own BeginOverlap, I invoke my delegate bind to the function with one parameter as you can see.

For more infos on delegate you can here : [Delegate doc](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Delegates/index.html)

Is my example help you ?

If not, or if you need anything, just tell me.

gamer08  
.

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 8, 2015, 8:46pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/5 "2015-08-08T20:46:35Z")

</div>

Thank you. I’ll test this code tomorrow

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 9, 2015, 11:12am UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/6 "2015-08-09T11:12:28Z")

</div>

It works on events like that:

![53342-zrzut+ekranu+2015-08-09+12.58.42.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/9/f/d9f7bb2a1c159670a86bc7715dd981fb3c74f52c.png)

but OnClicked doesn’t return anything.

![53343-zrzut+ekranu+2015-08-09+13.00.08.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/d/8/0d8a554a2b548eb80625bf82817945395fbe9fd9.png)

```
void UMasterUI_Menu::OnButtonClicked() //same for all buttons, binded inside while loop at runtime
{
	testdelegate.Execute(/*still no idea how i can get clicked button here*/);
}

```

Please, help me. Maybe there is another way of doing that.

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 12:55pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/7 "2015-08-09T12:55:14Z")

</div>

Hello XadE,

I have a little question for you, did you made your menu scrollBox in slate code or with blueprints directly?

if so, did you declared a custom class for your buttons in code?

such as

```
UCLASS()

class LoadButton : public UButton
{

/ **** /
/ **** /

};

```

Depending on your answers, I have different solutions for you.

gamer08

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 9, 2015, 1:04pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/8 "2015-08-09T13:04:04Z")

</div>

Hello gamer08,

For now it’s simple UScrollBox with UButtons spawned in c++ code(default class). I have widget blueprint too, with some panel widgets (it’s easier to make good menu design here).

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 1:23pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/9 "2015-08-09T13:23:10Z")

</div>

Ok thanks for that info, what I propose you is to create your own UButton subclass (that contains slate stuff) so you will be able to define your delegate attach to the Onclicked Event on your button and with that method, you’ll be able to catch the infos you need and use the code I shown you.

Did you follow me ?

I could also make an example for you if you need (with button this time to stick to your problem)

Just tell me, always glad to help.

gamer08

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 9, 2015, 1:33pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/10 "2015-08-09T13:33:29Z")

</div>

Hello gamer08,

Could you make an example? Thank you for response

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 1:39pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/11 "2015-08-09T13:39:43Z")

</div>

Ok i’ll do it, give me some time.

gamer08

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 3:11pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/12 "2015-08-09T15:11:04Z")

</div>

Hello, i’m building your example and I wanna know your UMasterUI\_Menu is a custom class you made?

What’s the type of it? I just want to know a bit of your achitecture to stick as most as possible on your situation.

For the moment I builded a custom UserWidget that has a ScrollBox but before going any further, I’d like to know your actual architecture related to your UI.

Thanks

gamer08

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 9, 2015, 3:57pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/13 "2015-08-09T15:57:13Z")

</div>

It’s custom UUserWidget

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 4:16pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/14 "2015-08-09T16:16:37Z")

</div>

Did you put your UMasterUI\_Menu on the gameMode, a customHUD or elsewhere?

Also can you send me a partial header and source (all I want, it’s your constructor and the init) of your UMasterUI\_Menu. My example is a bit fuzzy and Don’t want to lost you. If you don’t want, I’ll explain the best I can.

thanks gamer08

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 9, 2015, 4:23pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/15 "2015-08-09T16:23:45Z")

</div>

UCLASS()  
class EASSETH\_API UMasterUI\_Menu : public UUserWidget

It’s parent class of my widget blueprint which I add to viewport on BeginPlay of my menu pawn.

```
void UMasterUI_Menu::NativeConstruct()
{
	Super::NativeConstruct();

	menuPhase = EMenuPhaseEnum::VE_Menu;

	MasterUI_Construct();
}

```

MasterUI\_Construct - i use this function to read game video/audio settings from .sav and get ±10 widgets from widget blueprint (GetWidgetFromName).

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 4:30pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/16 "2015-08-09T16:30:59Z")

</div>

Thanks for sharing, i’ll be back soon

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 9, 2015, 4:38pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/17 "2015-08-09T16:38:59Z")

</div>

It’s my prototype function that spawns buttons.

Thanks, XadE

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 5:35pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/18 "2015-08-09T17:35:39Z")

</div>

my class exemple is almost done and now I’ve spotted the problem.

---

<div class="post-metadata">

**Author:** ![gamer08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamer08](https://forums.unrealengine.com/u/gamer08)\
**Post date:** [August 9, 2015, 6:03pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/19 "2015-08-09T18:03:06Z")

</div>

Header File

```
#pragma once

#include "Components/Button.h"
#include "DemoButton.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FLoadDelegate, FString, name);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FClickDelegate);

UCLASS()
class FPS4_8_API UDemoButton : public UButton
{
	GENERATED_BODY()

public:
	UDemoButton();

	UPROPERTY()
	FLoadDelegate load;
	
	UPROPERTY()
	FClickDelegate click;

	void OnClick();

	void LoadGameFile(FString name);
};

```

Source file

```
#include "FPS4_8.h"
#include "DemoButton.h"

UDemoButton::UDemoButton()
{
	OnClicked.AddDynamic(this, &UDemoButton::OnClick);
	
	//Bind function
	load.AddDynamic(this,&UDemoButton::LoadGameFile);
}

void UDemoButton::OnClick()
{
	load.Broadcast(this->GetName());
}

void UDemoButton::LoadGameFile(FString name)
{
	/ **load stuff here** /
}

```

So each button has his delegate and when you clicked, it will fire up.

You could also make a delegate that return stuff from your button and after call the load delegate from these info.

Is that help you more ?

If not just tell me.

gamer08

---

<div class="post-metadata">

**Author:** ![XadE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XadE](https://forums.unrealengine.com/u/XadE)\
**Post date:** [August 9, 2015, 6:17pm UTC](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041/20 "2015-08-09T18:17:21Z")

</div>

load.Broadcast(this-\>GetName());

I can’t test that today, so I have one more question. This line returns Name of clicked button and sends Name to loadgame function, right?

[Next page](https://forums.unrealengine.com/t/bind-onclicked-events-to-buttons-created-at-runtime/328041.md?page=2)
