Import blueprint class from C++

In a c++ class actor I have a

UWidgetComponent* Menu;

. I want to show in that component a widget create using Blueprint. The widget is simply a sublass of UMG.UserWidget called DebugMenu.

From the editor I can simply set WidgetClass to DebugMenu. I want to do the same from c++, using UWidgetComponent::SetWidgetClass. It requires an object of type TSubclassOf<UUserWidget> InWidgetClass

Can you show an example of how to call that method and what to include in order to reference the class DebugMenu?

Do you got blueprint for your actor class?

Here is sample code that might help, MyWidget is set in the editor then instantiated where ever you need it in your code.

.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Widgets”)
TSubclassOf MyWidget;

.cpp

if(MyWidget){
auto widget = CreateWidget(this, MyWidget);
widgetComp->SetWidget(widget);
}

The actor Is c++ only. I want here to show a blueprint widget.

I would want to set this in c++. Maybe using the reference string. Is it possible?
I don’t want to mess with settings. The actor should be ready to go as soon as you import it in the level

The actor classe Is c++ only

This solution is the same as setting directly the UWidgetComponent in the editor. I wanted a c++ solution that does not need modifications in the editor in order to work.

You need something like:
To get path you right click the widget in the editor and Copy Reference.

Cast(StaticLoadObject(UUserWidget::StaticClass(), NULL, *path)

You can still achieve that, you create a BP of your Character, in the World Settings of the level you will set your Character BP as the starting character. Then in you char BP you are able to setup all the TSubObject<> properties and the character will load up with everything on BeginPlay

The problem with this approach is that if you want to change the Widget you need to recompile after changing the code, unless of coarse you create a UPROPERTY with the reference string in which case it is far better to use a TSubObject