Here’s how I did this:
I created a new FTimerHandle + Delegate to tick down a float variable of GameStartCountdown
I expose a function called GetGameStartCountdown to Blueprints, then have the blueprint check on Event Tick for the value of GameStartCountdown.
In ToonTanksGameModeBase.h:
protected:
void DecrementGameStartCountdown(float TimeValue);
UFUNCTION(BlueprintCallable)
float GetGameStartCountdown();
private:
double StartDelay = 3.f;
float GameStartCountdown = 3.f;
FTimerHandle GameStartCountdownTimerHandle;
In ToonTanksGameModeBase.cpp:
void AToonTanksGameModeBase::DecrementGameStartCountdown(float TimeValue)
{
if(GetGameStartCountdown() >= 0)
{
GameStartCountdown -= TimeValue;
}
else
{
GetWorldTimerManager().PauseTimer(GameStartCountdownTimerHandle);
}
}
float AToonTanksGameModeBase::GetGameStartCountdown()
{
return GameStartCountdown;
}
void AToonTanksGameModeBase::HandleGameStart()
{
Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));
ToonTanksPlayerController = Cast<AToonTanksPlayerController>(UGameplayStatics::GetPlayerController(this, 0));
StartGame();
if(ToonTanksPlayerController)
{
ToonTanksPlayerController->SetPlayerEnabledState(false);
FTimerHandle PlayerEnableTimerHandle;
FTimerDelegate PlayerEnableTimerDelegate = FTimerDelegate::CreateUObject(ToonTanksPlayerController, &AToonTanksPlayerController::SetPlayerEnabledState, true);
GetWorldTimerManager().SetTimer(PlayerEnableTimerHandle, PlayerEnableTimerDelegate, StartDelay, false);
//New Timer Countdown to handle the Game Start UI Countdown via Blueprint
FTimerDelegate GameStartCountdownTimerDelegate = FTimerDelegate::CreateUObject(this, &AToonTanksGameModeBase::DecrementGameStartCountdown, 1.f);
GetWorldTimerManager().SetTimer(GameStartCountdownTimerHandle, GameStartCountdownTimerDelegate, 1.f, true, 1.f);
}
}
In WBP_StartGameWidget:
You could also just subtract DeltaTime from GameStartCountdown in the Tick function… but I wanted to pratctice the delegates =)
I wonder if there is a way to subscribe a C++ delegate to a blueprint function