Opening a Widget Blueprint from C++ Code

Hey,

I don’t yet managed it to open a widget User Interface from Code. What steps do I need to do? I red about two possible ways, BlueprintImplementableEvent and MulticastDelegate, where the latter seems a bit overkill. I just want to either send a function-call to my level-blueprint to open it, or (favoured) just create it directly via Code.

I included all the UMG / Slate headers and added the Dependencies in the C# file, but don’t know which parameters I have to pass “CreateWidget”.
The web says

CreateWidget<BlueprintClassName>…, but my BlueprintClassName is a unknown type in C++. Also, FindObject doesn’t work since the element between the <…> “has to static”. What am I doing wrong?

This are the things that worry me the most in UE4… That almost nothing is explained for C+±Programmers :frowning:

FStringClassReference MyWidgetClassRef(TEXT(“/Game/NewWidgetBlueprint.NewWidgetBlueprint_C”));
if ( UClass* MyWidgetClass = MyWidgetClassRef.TryLoadClass<UUserWidget>() )
{
UUserWidget* MyWidget = CreateWidget<UUserWidget>(World / GameInstance / PlayerController, MyWidgetClass);
// Do stuff with MyWidget
}

5 Likes

Thanks man :slight_smile:

This is a good way to shoot yourself in the foot though, you can encounter problems with cooking if you use string references, but fail to do things like load the assets when cooking. So make sure as long as you’re doing this that it’s loaded in the CDO of some class loaded when cooking, or loaded via some other code executed during cook, or that the folder containing your widgets is setup in your project settings as one to always cook.

In order to avoid this, some people create UPROPERTY’s of TSubclassOf<SomeClass> or TAssetSubclassOf<SomeClass> and make blueprints that configure all the asset references. Like maybe subclassing UGameInstance, exposing all your configurable UI properties there, and then subclassing it as a blueprint for configuration. It’s a handy way to decouple the C++ code from the asset/referencing. In case you’re avoiding blueprints entirely due to perf concerns, data only blueprints (blueprints that just override and configure property values), are not really any different in terms of speed than their C++ counterparts.

I just wanted to add for anybody landing from Google, you will also need to add to the viewport and enable input and mouse cursor.



    // Where menu might be your widget.
    Menu->AddToViewport();

    // Step 0 get player controller.
    auto PlayerController = GetFirstLocalPlayerController();
    if (!ensure(PlayerController != nullptr)) return;

    // Step 1 setup an input mode. There are multiple such as game only or game and UI as well.
    FInputModeUIOnly InputModeData;
    // Step 2 config is specific to the type
    InputModeData.SetLockMouseToViewport(false);
    InputModeData.SetWidgetToFocus(Menu->TakeWidget()); //Because UMG wraps Slate

    // Step 3 set the mode for the player controller
    PlayerController->SetInputMode(InputModeData);

    // Step 4 enable cursor so you know what to click on:
    PlayerController->bShowMouseCursor = true;


[HR][/HR]
Sam Pattuzzi - Co-instructor on the Udemy’s Best Selling Unreal Course