How to "combine" multiple UMG Blueprints loaded by C++ code

I have a question I couldn’t find an answer to, but probably I am just searching for the wrong keywords given that I am pretty new to UMG.

I have layed out my “static” UI in one UMG blueprint, looks fine. I am adding it to my level with C++ (cannot remember the exact way how I implemented it at the moment, but thats not so important).

I am now in progress of creating a dialog system that should spawn a Dialog message when a certain event happens in game. Now, I would like to lay out the Dialog message in a seperate UMG Blueprint, and then load the blueprint, populate the fields, and add it to the existing UI with C++.

Is it possible to load multiple UMG Blueprints in the same game? How can I add it in C++? Is there some example I missed?

UMG Blueprints work very similar to Actor or Object blueprints. You have the blueprint asset itself in the editor and a generated class which you can load in C++ (or other blueprints). You can have as many UMG widgets in your game as you want. To make a reference to a UMG widget class in C++ you need to create a property like so:



UPROPERTY(EditAnywhere)
TSubclassOf<UUserWidget> MyWidgetClass;


Then you need to set the class, which is easiest if you have a blueprint of the above class then you can set the MyWidgetClass in the editor defaults panel of that Blueprint. Then you can spawn that widget in C++ like so:



UUserWidget * MyWidget = CreateWidget<UUserWidget>(GetWorld()->GetGameInstance(), MyWidgetClass);


In order to add a widget to another widget in C++ you should have your main widget inherit from a custom UserWidget class which can have a blueprint implementable event to add a dialog widget to any slot widget.

Hope that gets you started.

Okay, that goes in the direction I hoped it would. Talking about a custom UserWidget class, so basically I could extend for example a Retainerbox to spawn and destroy (or show and hide in case of pooling being better for performance) messages when an event is received, and handle all the dirty details like i18n and the animation of the widgets in that custom class…
And then add that custom widget class to the UMG Blueprint?

Probably works, just to be sure I am on the right track

There is a difference between UWidget and UUserWidget. UWidgets are implementations for specific widgets like buttons, text boxes and retainer boxes. UUserWidgets are the base class for the UMG blueprints that you can editor in the engine. You can make a custom class inheriting from a retainer box but that’s probably not the solution your are looking for. A good approach is to separate the design from function, so you can freely edit your widget in the editor without worrying about the backend code functionality. So in short your custom widget classes should inherit from the UserWidget class, after compiling you can open the UMG blueprint, open the class settings and select your new C++ class as the parent class. In your C++ class you can have blueprint implementable events and blueprint callable functions to act as the interface between C++ and Blueprint, for adding the dialog box I would do something like



UFUNCTION(BlueprintImplementableEvent)
void AddDialogBox(UUserWidget * DialogBoxWidget);


So you can call the function from C++ and let the blueprint decide where and how to add it to the widget hierarchy.