So I wanted a single reusable function to generate simple SButton widgets. I was having trouble figuring out how best to pass the function reference as an parameter. After playing around a bit I came up with this by assigning the function to an auto variable and looking at the result. It seems to work fine but I am confused about some of the syntax.
TSharedRef<SHorizontalBox> SMyWidget::ConstructButtonLayout()
{
TSharedRef<SHorizontalBox> HorizontalBox = SNew(SHorizontalBox)
+SHorizontalBox::Slot()
[
ConstructButton(TEXT("My Shiny Button"), &SMyWidget::OnMyShinyButtonClicked)
];
return HorizontalBox;
}
TSharedRef<SButton> SMyWidget::ConstructButton(const FString ButtonText, FReply(SMyWidget::* Callback)())
{
return SNew(SButton)
.Text(FText::FromString(ButtonText))
.VAlign(VAlign_Center)
.HAlign(HAlign_Center)
.OnClicked(this, Callback);
}
FReply SMyWidget::OnMyShinyButtonClicked()
{
UE_LOG(LogMyWidget, Error, TEXT("The %s button works!"), TEXT("My Shiny Button"));
return FReply::Handled();
}
The issue I have is that I am not quite sure how I should be understanding this parameter inside the ConstructButton function FReply(SMyWidget::* Callback)()
. I would’ve expected it to look more like SMyWidget::* Callback
or even something more generic than that.
So I have three questions:
- How should I read this function parameter?
FReply(SMyWidget::* Callback)()
- Is that the most appropriate way to pass a function as a parameter? If not, how should it be done?
- If this is the correct way, is it possible to make it class agnostic?