How to convert a UObject to UUserWidget?

I have some old code from about 3 years ago, which I’m trying to update to work with the current engine. Only problem is the cast don’t seem to work the same as they used to. How do you cast a UObject for use with CreateWidget?

Relevant class and methods:

UCLASS()
class DIALOGUESYSTEM_API ADialogueHUD : public AHUD
{
	GENERATED_BODY()
	
public:
	virtual void PostInitializeComponents() override;
	void SwitchToDialogue(DIALOGUE_MODE dialogue_mode);
	
private:
	UPROPERTY()
	UObject *u;
	UObject *banter;
	UPROPERTY()
	UBaseDialogueHUD *dialogueHUD;
	
};

void ADialogueHUD::PostInitializeComponents()
{
	Super::PostInitializeComponents();
	
	u = LoadObject<UObject>(nullptr,
                        TEXT(DIALOGUE_DIALOGUE_HUDU));
	banter = LoadObject<UObject>(nullptr,
					TEXT(DIALOGUE_BANTER_HUDU));
}

void ADialogueHUD::SwitchToDialogue(DIALOGUE_MODE dialogue_mode)
{
	switch (dialogue_mode)
	{
		case CHOICE:
			if (u)
			{
				dialogueHUD = CreateWidget<UUDialogueHUD>((), Cast<UBlueprint>(u)->GeneratedClass);
				dialogueHUD->AddToViewport();
			}
			break;
		case BANTER:
			if(banter)
			{
				dialogueHUD = CreateWidget<UBanterHUD>((), Cast<UBlueprint>(banter)->GeneratedClass);
				dialogueHUD->AddToViewport();
			}
			break;
		default:
			UE_LOG(LogDialogue, Error, TEXT("Dialogue isn't found."));
			break;
	}
}

The specific error I get is

error: no matching function for call to 'CreateWidget'
dialogueHUD = CreateWidget<UUDialogueHUD>((), Cast<UBlueprint>(u)->GeneratedClass);
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/unreal-engine/Engine/Source/Runtime/UMG/Public/Blueprint/UserWidget.h:1459:10: note: candidate function template not viable: no known conversion from 'TSubclassOf<class UObject>' to 'TSubclassOf<UUserWidget>' for 2nd argument
WidgetT* CreateWidget(OwnerT* OwningObject, TSubclassOf<UUserWidget> UserWidgetClass = WidgetT::StaticClass(), FName WidgetName = NAME_None)

I’m trying to not break functionality; so, if possible I would prefer to not change UObjects to UUserWidgets till getting this to open in a project.

Instead of using UBlueprint type on bunter varable property use TSubclassOf<UUserWidget> instead.

Blueprints are classes too, once they loaded in asset registry (which happens on startup of engine) they are visible in reflection system and UClass object for them are made. CreateWidget expect you to give UClass* of class of widget, so why not directly give to it? TSubclassOf is that limits class selection to specfic relation in this cast UUserWidget, all Widget Blueprints generate UUserWidget class so you will see them all in property editor.

Yeah, using TSubclassOf for class properties seems like the right way to go. The only problem is I can’t use LoadObject to load the HUD asset. Is there a way of accessing the Underlying pointer of a UClass safely for LoadObject?

I do need that HUD asset, and I remember there being a reason I couldn’t just make it a blueprint in the first place. This used to just work as a workaround. In another part of the code during engine start up I generate the default asset, and then hook it up here.