I have an UUserWidget derived C++ class that has a BlueprintCallable method that finds and assigns pointers to UMG widgets created in a Blueprints “class”/asset derived from my C++ class, this in turn calls the aforementioned method after construction has finished.
- All pointers are valid and point to their respective widgets.
- In the above method I also construct buttons inside an UUniformGridPanel and they are correctly displayed when the Blueprint asset is added to the Viewport.
Here’s the problem:
When I add a function to the button’s OnClicked delegate after I create them (in a nested for loop to create rows and columns), The function only gets called for the first button of each row, with the exception of the final row, on which all buttons act like they should.
What am I doing wrong?!
Code:
uint8_t row = 0, col = 0;
uint8_t numCols = 4;
uint8_t numRows = 6;
for (row = 0; row < numRows; row++)
{
for (col = 0; col < numCols; col++)
{
// Construct Widget:
UButton* button = WidgetTree->ConstructWidget<UButton>(
UButton::StaticClass(), // Class to construct.
*FString::Printf( // Widget name.
TEXT("GridButton_%s"),
*FString::FromInt(row + col)
)
);
// Set cursor response:
button->SetCursor(EMouseCursor::Hand);
// Bind our OnClicked event:
button->OnClicked.Clear();
button->OnClicked.AddDynamic(this, &UUserInterfaceWidget::Clicked);
// Add it to the Uniform Grid and configure the slot:
UUniformGridSlot* slot = UniformGrid->AddChildToUniformGrid(button);
slot->SetRow(row);
slot->SetColumn(col);
// Keep track of it in our array:
Buttons.Add(button);
}
}