I am trying to create a super basic widget in C++
I want a canvas that holds just a black image that covers all the screen.
I dont know what is missing but I create an instance and add to viewport and nothing
is there. Also I create a blueprint user widget derived from my C++ widget
and the WBP is empty too.
I know I can create a child BP widget and bind components to access from code but as the widget is so simple I just want to avoid the BP side for this.
In NativeConstruct() add WidgetTree = NewObject<UWidgetTree>(); at the very top, this is becuase in some UE version widget tree might be null, this is not the case here because if so the engine would crash but anyway…
I think you have to wrap your (UUserWidget) with UWidgetComponent so the widget would get processing power, not really need to create new files but in the .h add
class YOUR_API USomeWidgetComponent : public UWidgetComponent
{
GENERATED_BODY()
public:
USomeWidgetComponent(const FObjectInitializer& ObjectInitializer);
virtual void BeginPlay() override;
};
at the very top, now add to your .cpp file
add these lines from before
Now create an actor with this component attached and run the game, is it working ?
if Yes, than your widget is OK, its just you can’t add it to viewport by itself - it should be wrapped with widget component (this is the only way i managed to create pure cpp widget).
If it’s not working i suspect that the problem is with the UCanvasPanel, try to make the root UOverlay, may that will do the trick.