Unreal 5.0 C++ Developer course, toon tanks game, countdown

Hi!

I am at the part where we do the countdown to the starting of the game inside the widget class. However, we do the same countdown inside c++. This is not practical at all. We have the same functionality twice that are completely independent of each other. What if i change the countdown time inside my c++ but forget to change it inside my widget? Getting the start time for the countdown inside the widget should come from the c++ file.

1 Like

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 :thinking:

1 Like

What I did was that I created the countdown variable in the widget and checked these boxes.
image

And afterwards I could set it in the StartGame Function like this.

And all you need to do is to make sure that StartDelay is exposed to blueprint and if it’s private set it’s meta to AllowPrivateAccess = “true”

Hope this helps!

4 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms