I’ve searched everywhere and I’m honestly amazed that I seem to be the first and only person to have encountered this issue. I have an ai character that I spawn multiple of in c++ code. This AI character, called “badguy2”, is also written in c++. The original AI has a widget that displays a healthbar, and the widget is a blueprint widget based off of c++ code.
The problem is that someone had the bright idea to make UMG widgets blueprint only and I think that causes an issue with instancing widgets in c++ so that when the AIs are spawned new widget instances are not.
I spawn the AIs by:
Abadguy2* a = GetWorld()->SpawnActor<Abadguy2>(Abadguy2::StaticClass(), GetActorLocation(), FRotator(0.0f, 1.0f, 0.0f), spawnPs);
In the AI code I try to create new instances of the widgets by:
// In Constructor
hWidget = CreateDefaultSubobject<UWidgetComponent>("HealthWidget");
/.../
// In BeginPlay()
hWidget->SetWidget(CreateWidget<UUserWidget>(GetWorld(), hBar));
Where hBar
is the blueprint widget class. Then I set the percent (health) of the widget in “badguy2.cpp” by:
Cast<UHealthBar3>(hWidget->GetWidget())->SetHealthPercent(health / 100.0f, healthid);
which calls this in the c++ base code of the HealthBar3 widget blueprint:
float UHealthBar3::GetHealthPercent(int healthid) {
healthPercentBar->SetPercent(percent);
}
The problem is that all of the health bars are the same because there is ONLY ONE WIDGET INSTANCE. I proved this by changing the code such that the widget c++ code would store an array of all of the health percents, then in each blueprint widget they would set their percent based on which widget they were but the only one that worked was the first healthbar because it was the only instance so the only one holding data.
My main issue is that the AIs are spawned in c++ code but that seems to not actually spawn new component instances so all of the AIs reference the same widget instance.
What do I do?