Download

How to Know if an UObject is a UUserWidget

Hello, i need to know if an object is a widget, i tryed :


if(objectToTest->IsA(UUserWidget::StaticClass()){}

That does not compile. the problem is in getting the Uclass of the UUserWidget, the folowing does not compile neither :


UClass* _class = UUserWidget::StaticClass();

Error from VS :

Does anyOne know why this is not working? Or if i can use an other way to know if an UObject is a Widget?

using Cast<> ???

Try the general C++ way



UUSerWidget* widget = static_cast<UUserWidget*>(object);
if(widget)
{
... //do your stuff if it's a widget
}


or the Unreal way



UUSerWidget* widget = Cast<UUserWidget>(object);
if(widget)
{
... //do your stuff if it's a widget
}


Of course, if you are absolutely 100% sure that it is always a valid widget you can skip the null check


if(widget) {}

and just call widget stuff (members/functions) on the casted UUserWidget pointer without the if-check.



UUSerWidget* widget = Cast<UUserWidget>(object);
... //if you are sure it's always a valid widget, you can stuff here without an if(widget)-check


You need to add UMG to your module’s dependencies, in Build.cs.

LNK2019 is usually always a Module include missing in Build.cs, yep