Move timer binding to code timer

This commit is contained in:
baz 2024-11-17 21:43:49 +00:00
parent 366dbbfda8
commit a13cad7320
5 changed files with 34 additions and 4 deletions

BIN
Content/Widgets/HUD/BP_HUDWidget.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -32,6 +32,8 @@ void AVampirePlayerController::OnPossess(APawn* aPawn)
gamemode->OnEnemyDeathCountIncrementDelegate.AddDynamic(this, &AVampirePlayerController::UpdateKillCountHUD); gamemode->OnEnemyDeathCountIncrementDelegate.AddDynamic(this, &AVampirePlayerController::UpdateKillCountHUD);
UpdateKillCountHUD(gamemode->GetEnemyDeathCount()); UpdateKillCountHUD(gamemode->GetEnemyDeathCount());
} }
GetWorld()->GetTimerManager().SetTimer(pawnLifeTimeHandle, this, &AVampirePlayerController::UpdateTimerHUD, 1.0f, true,0.f);
if (currentPlayerHUD) if (currentPlayerHUD)
{ {
@ -56,6 +58,14 @@ void AVampirePlayerController::UpdatePlayerLevelHUD(int level)
} }
} }
void AVampirePlayerController::UpdateTimerHUD()
{
if (currentPlayerHUD)
{
currentPlayerHUD->UpdateTimerBlock(GetPawn());
}
}
void AVampirePlayerController::UpdateKillCountHUD(int killCount) void AVampirePlayerController::UpdateKillCountHUD(int killCount)
{ {
if (currentPlayerHUD) if (currentPlayerHUD)

View File

@ -24,6 +24,8 @@ private:
TObjectPtr<UHUDWidget> currentPlayerHUD = nullptr; TObjectPtr<UHUDWidget> currentPlayerHUD = nullptr;
FTimerHandle pawnLifeTimeHandle;
protected: protected:
virtual void OnPossess(APawn* aPawn) override; virtual void OnPossess(APawn* aPawn) override;
@ -33,6 +35,9 @@ protected:
UFUNCTION() UFUNCTION()
void UpdatePlayerLevelHUD(int level); void UpdatePlayerLevelHUD(int level);
UFUNCTION()
void UpdateTimerHUD();
UFUNCTION() UFUNCTION()
void UpdateKillCountHUD(int killCount); void UpdateKillCountHUD(int killCount);
}; };

View File

@ -19,8 +19,23 @@ void UHUDWidget::UpdateLevelBlock(int level)
LevelBlock->SetText(FText::FromString("LV" + FString::FromInt(level))); LevelBlock->SetText(FText::FromString("LV" + FString::FromInt(level)));
} }
void UHUDWidget::UpdateTimerBlock() void UHUDWidget::UpdateTimerBlock(APawn* pawn)
{ {
int timeSinceCreation = FMath::FloorToInt(pawn->GetGameTimeSinceCreation());
FString mins = FString::FromInt(timeSinceCreation / 60);
if (timeSinceCreation / 60 < 10)
{
mins = "0" + mins;
}
FString secs = FString::FromInt(timeSinceCreation % 60);
if (timeSinceCreation % 60 < 10)
{
secs = "0" + secs;
}
TimerBLock->SetText(FText::FromString(mins + ":" + secs));
} }
void UHUDWidget::UpdateKillBlock(int killCount) void UHUDWidget::UpdateKillBlock(int killCount)

View File

@ -39,7 +39,7 @@ public:
void UpdateLevelBlock(int level); void UpdateLevelBlock(int level);
UFUNCTION() UFUNCTION()
void UpdateTimerBlock(); void UpdateTimerBlock(APawn* pawn);
UFUNCTION() UFUNCTION()
void UpdateKillBlock(int killCount); void UpdateKillBlock(int killCount);