Hi, I’m trying to create dynamic buttons at runtime, but i need to pass a varable to the onClick function. This is my actual code:
// THE CODE THAT CREATE BUTTONS IN MY FUNCTION
...
for (auto& item : LoadedData.Items)
{
UButton *button = NewObject<UButton>(UButton::StaticClass());
FString ImagePath = item.Texture;
UTexture2D* Texture = Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *(ImagePath)));
FSlateBrush itemBrush = UWidgetBlueprintLibrary::MakeBrushFromTexture(Texture);
itemBrush.ImageSize.X = 200;
itemBrush.ImageSize.Y = 200;
itemBrush.DrawAs = ESlateBrushDrawType::Image;
FButtonStyle style;
style.SetNormal(itemBrush);
style.SetHovered(itemBrush);
style.SetPressed(itemBrush);
button->WidgetStyle = style;
button->OnClicked.AddDynamic(this, &UDetailsPanelWidget::WriteLog);
itemsBox->AddChild(button);
}
...
// THE ONCLICK FUNCTION
void UDetailsPanelWidget::WriteLog()
{
UE_LOG(LogTemp, Warning, TEXT("*** BUTTON CLICKED"));
}
If I want to pass a FString to my WriteLog function, how I can do that? I’ll try with no success a method like this:
// IN THE FOR LOOP
...
button->OnClicked.AddDynamic(this, &UDetailsPanelWidget::WriteLog, item.Title);
...
// THE WriteLog Function
void UDetailsPanelWidget::WriteLog(FString title)
{
UE_LOG(LogTemp, Warning, TEXT("*** BUTTON MESSAGE: %s"), title);
}
Thanks a lot.