Hello everyone,
I’m trying to wrap my (fairly complex) slate UI in a widget component attached to the player’s actor.
So i wrote a UUserWidget derived class, very simple, here it is in its full glory:
.h:
UCLASS()
class YAG_API UYag3DWidget : public UUserWidget
{
GENERATED_BODY()
public:
UYag3DWidget(const FObjectInitializer& ObjectInitializer);
void SetContent(TSharedRef<SWidget> InContent);
bool CheckMyWidget();
protected:
virtual TSharedRef<SWidget> RebuildWidget() override;
};
---------------------------------------------------------------------------
.cpp:
UYag3DWidget::UYag3DWidget(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{}
void UYag3DWidget::SetContent(TSharedRef<SWidget> InContent) { MyWidget = InContent; }
bool UYag3DWidget::CheckMyWidget() { return MyWidget.IsValid(); }
TSharedRef<SWidget> UYag3DWidget::RebuildWidget() { return MyWidget.Pin().ToSharedRef(); }
In the actor’s class constructor i declared a UWidgetComponent and an instance of this 3D widget as follow:
This3DWidget = ObjectInitializer.CreateDefaultSubobject<UYag3DWidget>(this, TEXT("3D widget"));
ThisWidgetComponent = ObjectInitializer.CreateDefaultSubobject<UWidgetComponent>(this, TEXT("widget component"));
All this compiles well, but as soon as i try to set the WidgetComponent’s widget, i get a crash:
This3DWidget->SetContent(YagHUD->YagHUDWidget.ToSharedRef());
if (This3DWidget->CheckMyWidget())
{
ThisWidgetComponent->SetWidgetClass(This3DWidget->GetClass());
ThisWidgetComponent->SetWidget(This3DWidget);
}
Where YagHUDWidget is the SCompoundWidget that contains all my UI.
As soon as the SetWidget function is called (no matter when), i get a crash when UUserWidget::OnWidgetRebuilt() is called because WidgetTree is empty:
I posted a question on the answerhub, but as it’s not a bug report and given that 4.10 has just been released, i suspect it won’t get much attention:
https://answers.unrealengine.com/questions/331845/how-to-use-uwidgetcomponentsetwidget.html
So i was wondering if anyone succeeded in using a UWidgetComponent and could help me in my holy quest for a full 3D UI
Sorry for the long post, just the result of a long struggle.
Cheers
Cedric