Download

Using LoadObject with TSubclassOf<UUserWidget>?

I’m trying to initialize an asset to the right asset so I don’t have to manually hook it up in the editor. My previous solution from a few years ago no longer compiles. So, I tried replacing UObject* with TSubclassOf<UUserWidget> But, I can’t assign to that pointer now. Is there any means of assigning to the pointer underlying u, without causing issues? Any other means of doing this?

#define DIALOGUE_DIALOGUE_HUDU "/Dialogue/UI/DialogueHUDU.DialogueHUDU"

UCLASS()
 class DIALOGUESYSTEM_API ADialogueHUD : public AHUD
 {
     GENERATED_BODY()
     
 public:
     virtual void PostInitializeComponents() override;
     void SwitchToDialogue(DIALOGUE_MODE dialogue_mode);
     
 private:
     UPROPERTY()
     TSubclassOf<UUserWidget> u;
     UPROPERTY()
     UBaseDialogueHUD *dialogueHUD;
     
 };
 
 void ADialogueHUD::PostInitializeComponents()
 {
     Super::PostInitializeComponents();
     
     u = LoadObject<UUserWidget>(nullptr,
                         TEXT(DIALOGUE_DIALOGUE_HUDU));
    // No longer works
 }
 
 void ADialogueHUD::SwitchToDialogue(DIALOGUE_MODE dialogue_mode)
 {
             if (u)
             {
                 dialogueHUD = CreateWidget<UUDialogueHUD>(GetWorld(), u);
                 dialogueHUD->AddToViewport();
             }
 }

My previous solution was just

UObject *u = LoadObject<UObject>(nullptr,
                         TEXT(DIALOGUE_DIALOGUE_HUDU));
dialogueHUD = CreateWidget<UUDialogueHUD>(GetWorld(), Cast<UBlueprint>(u)->GeneratedClass);