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

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 :slight_smile:

Thanks in advance!

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)
1 Like

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!

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

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();
}

Make a custom delegate?..