For your AOLCharacterPlayer, you should really get rid of the SettingBar function. Also remove the call from the widget.
Your widget should initialize like this:
void UOLHpBarWidget::NativeConstruct()
{
Super::NativeConstruct();
HpBarProgress = Cast<UProgressBar>(GetWidgetFromName(TEXT("HpBar")));
ensure(HpBarProgress);
}
Your AOLNPCBasic constructor should call the base class constructor.
AOLNPCBasic::AOLNPCBasic()
: Super()
{
...
}
I like how you’re setting up the widget in the NPC class and how you set up the delegate in BeginPlay(). You should do the same thing with your player character. This way, you don’t need the SettingBar function at all and you can safely remove it as mentioned above.
I’m not sure I understand what the issue is, but the fact that you said the log is being called 5 times is likely because you’re creating 5 widgets and this code:
APawn* Player = GetOwningPlayerPawn();
always returns the player character even if it’s on an NPC. So it will call SettingHpBar() 5 times on your player character. This is why you see the same log 5 times. Now, shouldn’t IsPlayerControlled() return false for the NPCs? You would think so. So I’m not sure what’s going on there. Did you correctly set the AIController on your NPCs?
Anyhow, try removing the SettingHpBar function and set up your character widget the same way you did with your NPC with the BeginPlay function. After that, your widgets should start working a lot better.
BTW, you don’t need an interface to call ChangeHp(). It’s virtual. You can just call it directly in the TakeDamage function.
this->ChangeHp();
Not sure what HpUpdateTarget is or how it was initialized, but you don’t need it for calling ChangeHp();