# Slate: How to pass a variable along in the OnClicked event for a button?

**URL:** <https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584>\
**Category:** UI\
**Tags:** button, parameter, unreal-engine, delegate, slate, CPP, question\
**Created:** [March 26, 2014, 10:42pm UTC](https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584 "2014-03-26T22:42:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReqPro\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ReqPro\_1](https://forums.unrealengine.com/u/ReqPro_1)\
**Post date:** [March 26, 2014, 10:42pm UTC](https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584/1 "2014-03-26T22:42:39Z")

</div>

As the title says, I’ve got a button that I can click and a function is called - however, the OnClicked declaration shows a delegate to be passed with FReply as return value and no parameters.

Now it’s absolutely essential that I can pass along a parameter to that delegate since I’m dynamically adding buttons while iterating through an array, and I need to pass a reference to the object in the array that the button represents so I can figure out which action to take.

Hopefully one of you guys have an answer to this 🙂

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [March 26, 2014, 11:18pm UTC](https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584/2 "2014-03-26T23:18:53Z")

</div>

You’re able to bind variables to a delegate; I can’t remember the exact syntax when doing this inside Slate (nor the names of the delegate), but it would be something along the lines of:

```
FOnClicked::CreateSP(this, &SMyWidget::OnClicked, MyVariableToBind)

```

MyVariableToBind would then be passed into SMyWidget::OnClicked as a parameter.

Slate might have the syntactic sugar to let you do that as (I’m not sure though, as I don’t often have to bind variables to a delegate):

```
.OnClicked(this, &SMyWidget::OnClicked, MyVariableToBind)

```

---

<div class="post-metadata">

**Author:** ![ReqPro\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ReqPro\_1](https://forums.unrealengine.com/u/ReqPro_1)\
**Post date:** [March 26, 2014, 11:37pm UTC](https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584/3 "2014-03-26T23:37:50Z")

</div>

Thanks again, Jamie, you’re a real lifesaver.

I actually tried something very similar to this but dcouldn’t get it to work properly so I abandoned it - I must’ve messed up my pointer or something.

It works like a charm now, though.

Thanks again!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_89f0aa1e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_89f0aa1e](https://forums.unrealengine.com/u/anonymous_user_89f0aa1e)\
**Post date:** [August 6, 2015, 4:39pm UTC](https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584/4 "2015-08-06T16:39:55Z")

</div>

How would I go about passing a parameter from a UMG UButton?  
It has a dynamic multicast delegate (without parameters though).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_42a36632](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_42a36632](https://forums.unrealengine.com/u/anonymous_user_42a36632)\
**Post date:** [October 30, 2015, 8:36am UTC](https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584/5 "2015-10-30T08:36:43Z")

</div>

This way works, but i needed some time to get it right,  
here is a longer description:

```
	FString f = FString("delegateString");
	FOnClicked::CreateSP(this, &SStandardSlateWidget::myFunc, f);

    SNew(SButton).OnClicked(this, &SStandardSlateWidget::myFunc, FString("OnClickedString"))

```

And the function:

```
FReply SStandardSlateWidget::myFunc(FString myString)
{
	if (GEngine)GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("f-delegate: %s"), *myString));
	return FReply::Handled();
}

```

---

<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, 10:26am UTC](https://forums.unrealengine.com/t/slate-how-to-pass-a-variable-along-in-the-onclicked-event-for-a-button/278584/6 "2017-06-16T10:26:04Z")

</div>

Make a custom delegate?..
