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>(GetWorld(), Cast<UBlueprint>(u)->GeneratedClass);
dialogueHUD->AddToViewport();
}
break;
case BANTER:
if(banter)
{
dialogueHUD = CreateWidget<UBanterHUD>(GetWorld(), 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>(GetWorld(), 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)