How do I get separate instances of progress bar in BP widget class to c++ widget class?

So I have a user widget c++ class called “HealthBar3”, and a BP user widget class called “HealthBar3BP” that is based off of the c++ class. The BP widget simply has a canvas panel with a progress bar in it, both of which are bound to the c++ class by UPROPERTY(meta = (BindWidget)) variables in the “HealthBar3.h” file:

UPROPERTY(meta = (BindWidget))
	UProgressBar* healthPercentBar;

UPROPERTY(meta = (BindWidget))
	UCanvasPanel* baseCanvas;

Now in my scene I have a character called “badguy2” who has a widget component, the widget class is set to a new created widget in “badguy2.cpp” to of the class HealthBar3BP:

hWidget = CreateDefaultSubobject<UWidgetComponent>("HealthWidget");

hWidget->SetWidget(CreateWidget<UUserWidget>(GetWorld(), hBar));

Where hBar is UPROPERTY(EditAnywhere) TSubclassOf<UUserWidget> hBar; This works fine.

When I spawn multiple of these “badguy2” characters they all have the HealthBar3BP widget, however, in “badguy2.cpp” I set the percent of the progress bar to be the health of “this” badguy2 character:

Cast<UHealthBar3>(hWidget->GetWidget())->SetHealthPercent(this->health / 100.0f);

in “HealthBar3.cpp” this does:

void UHealthBar3::SetHealthPercent(float percent) {

        healthPercentBar->SetPercent(percent);
	}
}

but all of the health bars have the same percent. So it works but they all seem to have the same progress bar.

I know exactly what is happening I just don’t know how to fix it. Basically every badguy2 has the HealthBar3BP widget, but for some reason there is only one instance of that widget shared across all badguy2s.

So my question is how do I get each spawned badguy2 to have its own instance of “HealthBar3BP”?

Looks like you are doing SetWidget(CreateWidget…) in constructor, which is not good. In constructor you should set the widget class only, and let widget component spawn its own widget at runtime.

BUT since widget class is blueprint, you can’t really set it in constructor, unless you use one of those shady FClassFinder or something. Instead, I would recommend simply pasting your hBar property value directly into the hWidget component defaults, and remove hBar variable as it is redundant.

Sorry I forgot to label that ‘hWidget’ is created in the constructor but ‘SetWidget’ is in ‘BeginPlay’.

The problem is coming from the fact that CreateWidget, doesn’t actually create new widgets it just references the one blueprint widget class somehow as the actual widget object.

Working example of your classes implemented with health bars

Right click on characters to damage.