How to Downcast from UUserWidget to derived widget class?

Hello,

In my Character class I have a Property for a crosshair widget:


UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
TSubclassOf<UUserWidget> CrosshairWidget;

On BeginPlay() I create the widget and store the reference:


CrosshairInstance = CreateWidget<UUserWidget>(GetWorld(), CrosshairWidget);
     if (CrosshairInstance)
         CrosshairInstance->AddToViewport();

Now, my question is how can I downcast from the UUserWidget instance to my derived class so I can access properties within it?

By the way, I would have just created the widget property as my derived class if I could have found a way to access the type, but it appears I can only access a base blueprint class from C++.

Thanks!

You cannot access Blueprint declared properties from within C++ without some serious hackery using the reflection system, iterating through all UProperty objects.

I figured sometime ago, although the visual editor is super nice and useful, that using UMG adds a lot of annoying problems to C++ side and I’d rather go pure Slate code as much as possible.

Making functions just to get variables from Blueprint side can get messy quickly, but that’s pretty what you have to do, create custom UUserWidget classes and add Get/Set functions everywhere for everything.

[USER=“434”]BrUnO XaVIeR[/USER] In this case, I was trying to use a widget that was already built. I guess another option is to re write the widget in C++ and call it a day. Since it utilizes the Tick() function, I’m probably better off using C++ anyway.

Thanks for the response.