When I try to retrieve the root widget in a UUserWidget it is always nullptr.
if (!Cast<UPanelWidget>(GetRootWidget())) {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, "omfg");
}
Similarly when I try to retrieve the slot of the root widget it always returns nullptr.
if (!Cast<UPanelWidget>(GetRootWidget())->Slot) {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, "ugh");
}
I first tried to create my widget by making the main canvas:
newCanvas = Cast<UCanvasPanel>(GetRootWidget());
But obviously that doesn’t work because GetRootWidget()
is nullptr
. So then I tried:
newCanvas = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass());
WidgetTree->RootWidget = newCanvas;
newCanvas->Slot = NewObject<UPanelSlot>();
UCanvasPanelSlot* baseSlot = Cast<UCanvasPanelSlot>(newCanvas->Slot);
baseSlot->SetSize(FVector2D(500.0f, 500.0f));
But obviously that gave me a read access violation
because the slot isn’t actually a part of a widget, and it can’t be because the root widget is * nullptr.
I see “omfg” and “ugh” on screen and that’s how I feel. How are you supposed to build a UUserWidget in c++ if you can’t even place widget objects in the widget?