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 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.
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);