# How to pass self-reference to UButton's OnClicked delegate (UMG)?

**URL:** <https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841>\
**Category:** UI\
**Tags:** button, unreal-engine, onclicked, delegates, question, CPP\
**Created:** [June 16, 2017, 1:41pm UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841 "2017-06-16T13:41:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![WHSolv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WHSolv](https://forums.unrealengine.com/u/WHSolv)\
**Post date:** [June 16, 2017, 1:41pm UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/1 "2017-06-16T13:41:01Z")

</div>

I’ve tried to create a custom OnClicked delegate for custom Button (inherited from UButton), that passes self-reference, but it is not exectued when I click a button.

Header:

```
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnButtonClickedRefDelegate, UExtendedButton*, ButtonRef);
    UCLASS()
    class DQS_API UExtendedButton : public UButton
    {
    	GENERATED_UCLASS_BODY()
    	
    public:
    	UExtendedButton();
    
    	/** Should be called when the button is clicked, also gives the reference to this... */
    	UPROPERTY(BlueprintAssignable, Category = "Button|Event")
    	FOnButtonClickedRefDelegate OnClickedReferenced;
    
    	UFUNCTION()
    	void OnSelfClicked();
    
    	UFUNCTION()
    	void DoSomething(UExtendedButton* ButtonRef);
    
    protected:
    	/** Handle the actual click event from slate and forward it on */
    	FReply SlateHandleClicked();
    
    };

```

Source:

```
UExtendedButton::UExtendedButton(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	OnClickedReferenced.AddDynamic(this, &UExtendedButton::DoSomething);
	OnClicked.AddDynamic(this, &UExtendedButton::OnSelfClicked);

	
}

void UExtendedButton::OnSelfClicked()
{
	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, 
			FString("Hello :: ").Append(this->ButtonText->GetText().ToString())
		);

	OnClickedReferenced.Broadcast(this);
}
void UExtendedButton::DoSomething(UExtendedButton* ButtonRef)
{
	OnClickedReferenced.Broadcast(ButtonRef);

	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red,
			FString("Do :: ").Append(ButtonRef->ButtonText->GetText().ToString())
		);
}

/* Cannot be overwritten since the original one isn't virtual... */
FReply UExtendedButton::SlateHandleClicked()
{
	OnClickedReferenced.Broadcast(this);

	return FReply::Handled();
}

```

I couldn’t set things right as in [here][1], since I didn’t expect to touch Slate at all, only UMG.  
What I want to achieve is this BP working:

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/a/2/ea2d0c857ea7560b9c26f716fcd607722b05f637.jpeg)

---

<div class="post-metadata">

**Author:** ![WHSolv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WHSolv](https://forums.unrealengine.com/u/WHSolv)\
**Post date:** [June 16, 2017, 5:34pm UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/2 "2017-06-16T17:34:20Z")

</div>

Solved it.  
In `UButton` we have `RebuildWidget()`, which we should override for custom `OnClicked`. We are interested in `MyButton = SNew(SButton) .OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SlateHandleClicked))... `, in which SlateHandleClicked should be `OnClickedReferenced.Broadcast(this)` instead of original one `OnClicked.Broadcast()`. To do it, I suppose, we need to have all the SlateHandle-functions (Clicked, Pressed, Hovered, etc.) implemented once more, or just to rewrite engine’s functions to be `virtual`.

The complete code for `RebuildWidget()`, ( `SlateHandleClicked()` stays the same as in the question post):

```
TSharedRef<SWidget> UExtendedButton::RebuildWidget()
{
	MyButton = SNew(SButton)
		.OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SlateHandleClicked))
		.OnPressed(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandlePressed))
		.OnReleased(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandleReleased))
		.OnHovered_UObject(this, &UExtendedButton::SlateHandleHovered) /* Be careful here... */
		.OnUnhovered_UObject(this, &UExtendedButton::SlateHandleUnhovered) /* And here... */
		.ButtonStyle(&WidgetStyle)
		.ClickMethod(ClickMethod)
		.TouchMethod(TouchMethod)
		.IsFocusable(IsFocusable)
		;

	if (GetChildrenCount() > 0)
	{
		Cast<UButtonSlot>(GetContentSlot())->BuildSlot(MyButton.ToSharedRef());
	}

	return MyButton.ToSharedRef();
}
```

---

<div class="post-metadata">

**Author:** ![Muzaheed](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/muzaheed/32/778940_2.png) [@Muzaheed](https://forums.unrealengine.com/u/Muzaheed)\
**Post date:** [August 1, 2017, 4:40am UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/3 "2017-08-01T04:40:41Z")

</div>

i have been using your code snippet and did implement it in my project. but no child umg under this specific Umg class is not getting rendered. as u can see in this video:

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/f/8/6f8b63d7c36c0a076125a853db45928da6b08c10.jpeg "gggg") ](https://www.youtube.com/watch?v=dTadlQX8I-o)

any idea?

nad thanks for the code snippets

---

<div class="post-metadata">

**Author:** ![WHSolv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WHSolv](https://forums.unrealengine.com/u/WHSolv)\
**Post date:** [August 1, 2017, 7:02am UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/4 "2017-08-01T07:02:23Z")

</div>

You need to add initialisation in constructor `UExtendedButton::UExtendedButton` like `ButtonText = CreateDefaultSubobject(_T("ButtonText"));`.

---

<div class="post-metadata">

**Author:** ![Muzaheed](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/muzaheed/32/778940_2.png) [@Muzaheed](https://forums.unrealengine.com/u/Muzaheed)\
**Post date:** [August 1, 2017, 10:41am UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/5 "2017-08-01T10:41:50Z")

</div>

not sure where this ButtonText coming from . but anyway thanks.

---

<div class="post-metadata">

**Author:** ![WHSolv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WHSolv](https://forums.unrealengine.com/u/WHSolv)\
**Post date:** [August 1, 2017, 12:32pm UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/6 "2017-08-01T12:32:54Z")

</div>

`ButtonText` is a `UTextBlock` variable in the header of `UExtendedButton`, if you want it to be there. If you have added TextBlock in UMG-editor, then it should be there by default. Double check Appearence and Behavior categories, e.g., it could be white text on white background or text could have 0 alpha.

---

<div class="post-metadata">

**Author:** ![Muzaheed](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/muzaheed/32/778940_2.png) [@Muzaheed](https://forums.unrealengine.com/u/Muzaheed)\
**Post date:** [August 27, 2017, 6:11am UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/7 "2017-08-27T06:11:18Z")

</div>

it turned out to be a issue caused by RebuildWidget(). now fixed

---

<div class="post-metadata">

**Author:** ![WHSolv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WHSolv](https://forums.unrealengine.com/u/WHSolv)\
**Post date:** [August 27, 2017, 8:28am UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/8 "2017-08-27T08:28:35Z")

</div>

What do you mean?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d8e790ff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d8e790ff](https://forums.unrealengine.com/u/anonymous_user_d8e790ff)\
**Post date:** [October 20, 2018, 1:44pm UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/9 "2018-10-20T13:44:27Z")

</div>

Hello! I’ve tride this method, but in runtime I catch an error, if I use RebuildWidget function. I’ve implemented all methods such as onclicked, onhoverd etc. What can be wrong here?

---

<div class="post-metadata">

**Author:** ![WHSolv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WHSolv](https://forums.unrealengine.com/u/WHSolv)\
**Post date:** [January 13, 2019, 10:43am UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/10 "2019-01-13T10:43:25Z")

</div>

It would be easier if I can replicate the particular issue. Can you share the code?  
If it’s still relevant, though.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d8e790ff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d8e790ff](https://forums.unrealengine.com/u/anonymous_user_d8e790ff)\
**Post date:** [January 13, 2019, 2:31pm UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/11 "2019-01-13T14:31:57Z")

</div>

Thank you! I’ve already done with it. Just needed to implement all virtual methods including onhover etc. I ignored them first because I didn’t need them. And when I implemented them, the errors disappeared.

---

<div class="post-metadata">

**Author:** ![WHSolv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WHSolv](https://forums.unrealengine.com/u/WHSolv)\
**Post date:** [January 13, 2019, 2:35pm UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/12 "2019-01-13T14:35:26Z")

</div>

Glad to hear that. Have a nice day then.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d8e790ff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d8e790ff](https://forums.unrealengine.com/u/anonymous_user_d8e790ff)\
**Post date:** [January 14, 2019, 7:19am UTC](https://forums.unrealengine.com/t/how-to-pass-self-reference-to-ubuttons-onclicked-delegate-umg/398841/13 "2019-01-14T07:19:22Z")

</div>

Thanks, and you too, man!
