Need help constructing a blueprint widget in C++

Hey all, so I have a blueprint widget that is the child of a C++ UUserWidget-derived class.

I’m trying to make this widget fairly global since I use it in every level (it fades to/from black on level load/exit), so I’m constructing it in my first level’s BP and saving a reference in my GameInstance.

The issue is, if I load a random level first, it won’t have the widget constructed. Thus, why I want to construct the BP from my C++ GameInstance. Just constructing the UUserWidget-derived class wouldn’t be enough, since the BP has animations that I need to use.

I’ve looked around, but the posts I’ve found seem to be slightly different applications. I can reference all of my necessary functions through the UUserWidget-derived class’s BlueprintImplementableEvents, I just need the blueprint widget constructed so that I actually have those events implemented.

Thanks!

I think that what you want is to create your Blueprint UMG widget derived from your C++ base class inside the C++ GameInstance, right?

If that’s what you want then you have multiple options:

The first way (and for me the cleanest):

Using UDeveloperSettings: UDeveloperSettings will auto-register itself with all of its UPROPERTY config variables inside the Project Settings panel. That way you can get those settings from anywhere in your C++ code using the CDO for your settings class.

Example:

UMyDevSettings.h

#pragma once

#include "CoreMinimal.h"
#include "Engine/DeveloperSettings.h"
#include "MyDevSettings.generated.h"

UCLASS(config = Game, defaultconfig)
class MYGAME_API UMyDevSettings: public UDeveloperSettings
{
	GENERATED_BODY()
	
public:

	UPROPERTY(config, EditAnywhere)
	TSubclassOf<UYourUserWidgetClass> MyUserWidgetClass;
}

My User Widget Class will appear as a configurable property in your Project Settings panel, in Game category. There you can set your UMG blueprint.

Then to get that blueprint class from your GameInstance use this:

// Include your dev settings header
#include "MyDevSettings.h"

.
.
.
.
.

// Where you want to get the class
TSubclassOf<MyUserWidgetClass> MyWidgetClass = GetDefault<UMyDevSettings>()->MyUserWidgetClass;

// Then you create your widget using that class that you previously set as the blueprint

The second way (simple but not so clean):

In your game instance in C++ add the property for the widget class just like we did in the dev settings class.

MyGameInstance.h

UPROPERTY(EditAnywhere)
TSubclassOf<UMyUserWidgetClass> MyUserWidgetClass;

Then create a blueprint inheriting from your C++ game instance and set the user widget class in there.
You will have to use your blueprint game instace instead of the C++ one and that way you will be able to access the widget class from your C++ game instance base class.

Please let me know if this is what you are looking for and please mark my answer as correct if it suits your needs.

Cheers