You should inherit from UWidget, not UUserWidget. UUserWidget is a special classification of widget that are designed to be inherited by the designer. If you’ve already got an entire UI (or large complex pieces) and you just want to build a wrapper for it that lets someone configure some stuff about it, UWidget is the way to go.
You should take a look at any of the UWidgets in the UMG module on github NativeWidgetHost is probably a good one to use as a reference; it’s pretty minimal.
I have tried this in the PostInitializeComponents() function of my actor:
if (GetWorld()->GetFirstPlayerController())
{
AYagHUD* YagHUD = Cast<AYagHUD>(GetWorld()->GetFirstPlayerController()->GetHUD());
if (!YagHUD) return;
GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, TEXT("hud ok, adding widget"));
// set the widget
ThisNativeWidgetHost->SetContent(YagHUD->YagHUDWidget.ToSharedRef());
}
I get the “hud ok” message, but the widget is not displayed and the WidgetCompoennt remains invisible (although i checked “opaque” and “twosided” in the editor)
YagHUDWidget is my mother widget, a large SCompoundWidget containing a ton of children.
If i manually assign a class to the component (from the editor), i can see it in game, so there is something wrong in my above code.
Or maybe shouldn’t it be in the PostInitializeComponents() ?
I’m going on with my tests, and will update if i find something, but any hint would be great.
I debugged my code and found that my UNativeWidgetHost was correctly containing my SCompounWidget, so it seems that i successfully created and populated a UNativeWidgetHost, all good.
But my UWidgetComponent has both its Widget and SlateWidget variables set to null.
In fact i realized that i had not assigned the native widget host to the widget component. No wonder it didn’t work !!
So i tried this promising direction but nowhere could i find a SetWidget() function in the widget component doc.
So i went into the code and i couldn’t find such a function in the public section. Moreover, the Widget variable is private, so i can’t see a way to assign a UWidget to a UWidgetComponent.
Going into the UE code i brand new to me, my skills are quite limited so maybe i missed something obvious ?
Currently, both roads to success seem closed:
i can’t have my SCompoundWidget appear in the UMG list so i can’t assign it to a native host widget in the UMG editor
i can’t assign my c++ created UNativeHostWidget to a UWidgetComponent
I watched the UEditableText code and understood i had to overwrite RebuildWidget so i copy pasted the native widget host into a class inheriting from UWidget (UYag3DWidget here) and changed this:
Using BuildDesignTimeWidget gives a crash, but returning just SNew() is ok.
My Yag3DWidget class is now visible in the umg designer list and i can now see it ingame.
But i am now facing 2 other problems:
i had to cut all initializations, so the 3D UI is all empty (i need ingame infos to initialize it properly). So i need to find a way to force the proper reconstruction in game.
it doesn’t respond to mouse. I have followed the “create 3D widget” tutorial in the doc, but with the same settings i couldn’t have my widget actor respond to mouse yet.