Cast UClass to UUserWidget

Hey !
Your solution is totally wrong.
You try to use template, but your function isnt templatized, uclass return is useless, return UUserWidget instead of UClass… GetClass isnt needed…

Of course you can use template class function, but for this first read how templates works: C++ Templates

Anyway i highly recommend to do this in way:

.h

UUserWidget* CreateAndAddWidget(APlayerController* OwningPC, TSubclassOf<UUserWidget> WidgetClass;

.cpp

UUserWidget* YourClass::CreateAndAddWidget(APlayerController* OwnerPC, TSubclassOf<UUserWidget> WidgetClass)
{
   If(!WidgetClass)
       return nullptr;

   UUserWidget* wid = CreateWidget<UUserWidget>(OwnerPC, WidgetClass);

   If(wid)
        wid->AddToViewport(1);

  return wid;
}

In this case you dont need cast and every widget is based on uuserwidget, so safe to give back uuserwidget :slight_smile:
Cheera