How to access UUserWidget-contained ProgressBa in C++

I have a C++ class inherited from ACharacter. This class has a UWidgetComponent* member that is used to display a health bar above the character. The widget class was a blueprint that contains only one progress bar, and it was chosen and set in the blueprint editor.

I know how to use Blueprint to cast it:

I want to update the health bar in C++, but I don’t know how to cast UUserWidget * to be the class that I can set the progressbar percent.

UUserWidget *widget = widgetComponent->GetUserWidgetObject();

//Any idea here? … to cast widget from UUserWidget to be UIndicator*

Thanks for your help,

I meet the same issue with you,do you resolve it finally?

It would help me too

I figured it out.
There is a member function named “GetWidgetFromName”,which returns a UWidget.
Find the widget progress bar’s name in Blueprint editor, then cast to UProgressBar(include “ProgressBar.h”).All is OK.

1 Like

There is a member function named “GetWidgetFromName” in UUserWidget which returns a UWidget.
Find the widget progress bar’s name in Blueprint, and cast the result to ProgressBar.Call the SetPercent function,That’s OK.


For example:

    UUserWidget *Widget = WidgetComponent_->GetUserWidgetObject();
    UProgressBar *ProgressBar = dynamic_cast<UProgressBar *>(Widget->GetWidgetFromName(FName("hpbar")));  // hpbar is the widget progress bar's name in Blueprint
    ProgressBar->SetPercent(0.1);
2 Likes

Thank you very very much, this solution works for ue5 as well!

UIndicator* IndicatorWidget = Cast<UIndicator>(widgetComponent->GetUserWidgetObject());
if (IndicatorWidget) {
  // success
}