is it possible to programmatically get all Widgets which are children of a custom UserWidget in c++?
like so:
void UMyUserWidget::MyCustomFunc()
{
// example
TArray<UWidget> Children = this->GetChildren();
}
Image i make a Widget Blueprint from UMyUserWidget and i add some Buttons in the Designertabs. And then GetChildren() would return those buttons (and other widgets of course).
Hm, i only found that “UPanelWidget” and all underlying classes have Child Operation (maybe other classes too, but i haven’t found anything within
the UUserWidget class):
Too always have your widgets at hand, you should just add them to an Array when you add them at all. I did this in UMG and would do this in C++ too.
When you add a series of widgets, add them to an array. All other widgets, like pre placed buttons or so, already should have a variable. Otherwise you wouldn’t
be able to interact with them
void UMyUserWidget::GetMyChildren()
{
MyChildrenArray.Empty();
// Get the root panel widget (if you know its a panel, otherwise cast to the widget you have as root)
if (UOverlay* OverlayWidget = Cast<UOverlay>(GetRootWidget()))
{
// in my case the root overlay has a verticalbox as child, and since SOverlay only has ONE child i simply get the first one at index 0
if (UVerticalBox* VertBox = Cast<UVerticalBox>(OverlayWidget->GetChildAt(0)))
{
// get the number of children inside the verticalbox to iterate through
int32 ChildrenCount = VertBox->GetChildrenCount();
for (int32 i = 0; i < ChildrenCount; i++)
{
// if the current child is of widgettype i want to save add it to the array
if (UMyDesiredChildWidget* btn = Cast<UMyDesiredChildWidget>(VertBox->GetChildAt(i)))
{
MyChildrenArray.Add(btn);
}
}
}
}
}
Maybe you have a better idea to do it, but i felt like to share my solution as well