First, I did it with Initialize(), but there was a crash, so I made it with the existing code in NativeConstruct().
void UOLHpBarWidget::NativeConstruct()
{
Super::NativeConstruct();
// 해당 컨트롤을 이름으로 찾기
HpBarProgress = Cast<UProgressBar>(GetWidgetFromName(TEXT("HpBar")));
ensure(HpBarProgress);
// CharacterBase의 함수 가져오기
APawn* Player = GetOwningPlayerPawn(); // Player 가져오기
ensure(Player);
IOLHpInterface* PlayerInterface = Cast<IOLHpInterface>(Player);
ensure(PlayerInterface);
PlayerInterface->SettingHpBar(this);
HpBarProgress->SetPercent(1.f);
UpdateHpBar();
}
I put a value of 1.f in SetPercent. The reason is that the Hp Bar should have been full of green when the game started, but it’s gray.
void UOLHpBarWidget::UpdateHpBar()
{
UE_LOG(LogTemp, Error, TEXT("Hp Loding..."));
APawn* Player = GetOwningPlayerPawn(); // Player 가져오기
ensure(Player);
IOLHpInterface* PlayerInterface = Cast<IOLHpInterface>(Player);
ensure(PlayerInterface);
UpdateHp = PlayerInterface->SetHp();
UE_LOG(LogTemp, Log, TEXT("UpdateHp: %f"), UpdateHp);
if (HpBarProgress)
{
HpBarProgress->SetPercent(UpdateHp);
UE_LOG(LogTemp, Error, TEXT("UpdateHp"));
}
else
{
UE_LOG(LogTemp, Error, TEXT("Nooooo HpBar"));
}
}
So when I checked it in this code, UpdateHp was 0. For your information, the SetHp function returns. returnHp / MaxHp; returns this value. But I don’t understand why it comes out as 0…