Nneeds before encountering a problem:
I need to create blueprint asset UserWidgets in c++, Widgets are created and implemented entirely from blueprints, I can customize the number and layout of widgets in c++;
Since ContructObjFormClass() in the blueprint can’t build the Widgets directly, I moved to implement them inside c++
My implementation idea:
First create a SDynamicWidget that inherits from SVerticalBox, load the blueprint asset and get his Class via LoadClass<>() in the Construct() function
UClass* TitleClass = LoadClass<UUserWidget>(WorldContent, WG_TITLE);
Then NEW a sample blueprint
UUserWidget* BlueprintInstance = NewObject<UUserWidget>(WorldContent, TitleClass);
Since the RootWidget of this object is Null, I initialize it and add it to the VerticalBox::FSlot.
if (BlueprintInstance)
AddSlot()
[
BlueprintInstance->GetRootWidget()->TakeWidget()
]
}
Then this UWidget will display the created UI:
BUT:
When the widget AddToViewport(), runs again for about 30 seconds before crashing:
I can’t find out anything about it on the call stack.
Restoration step:
- New UserWidget Asset
- Add any component
- New c++Class Public UWidget And SClass Public SVerticalBox
- Loading blueprint assets in SVerticalBox subclasses
UClass* PClass= LoadClass<UUserWidget>(nullptr, "/Game/Vendor/DynamicWidget/NewWidgetBlueprint.NewWidgetBlueprint_C'");
UUserWidget* BlueprintInstance = NewObject<UUserWidget>(WorldContent, PClass);
- Add Blueprint Widget to Slot
BlueprintInstance->Initialize();
AddSlot()
[
BlueprintInstance->GetRootWidget()->TakeWidget()
]
- Use it and AddToViewport
- Start Play, wait for crash
Engine version:4.27
UE5 didn’t change the UserWidget a lot, so it should be able to restore this in UE5 as well!