Opening widget blueprint from C++

How do I create a blueprint widget through C++. I found a few similar questions like this one ([link1][1], [link2][2]) but none work for me. After the code from the image below, the function is called in blueprint. I don’t get any errors, but all I get is a black screen after clicking the play button. If I change the UMyUserWidgetBase object name from widget to MyWidget (like the [first link][3] says), I get an error saying “MyUserWidgetBase.cpp(100) : error C4458: declaration of ‘MyWidget’ hides class member”. However, both don’t work, one just gives an error while the other does not.


I can see that in your line a few things:

FStringClassReference MyWidgetClassRef(TEXT("/Game/c++calsses/UI/PreLevelSelect.PreLevelSelect"))

  1. Based on the example provided in the first link there string reference ends with a _C, and yours doesn’t; maybe that is causing an issue here. Your string reference should read “/Game/c++calsses/UI/PreLevelSelect.PreLevelSelect_C”
  2. There is the misspelling of the word ‘classes’ in your string reference, so just be sure that the spelling matches the directory.

What is also referenced in that link is that you should try to use a UPROPERTY of TSubclassOf for a reference to your UI object. Here is an example from my personal project of this property that I have in my player controller .h:

// Reference UMG Asset in the Editor
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Hero|UI|Main")
TSubclassOf<class UHeroCharacterUIMain> HeroMainUI;

Then, I have this logic in my player controller cpp:

if (HeroMainUI)
	HeroMainWidget = CreateWidget<class UHeroCharacterUIMain>(GetGameInstance(), HeroMainUI);
	if (HeroMainWidget)

I hope this will help and good luck :slight_smile: