Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Latest commit

 

History

History
50 lines (42 loc) · 1.33 KB

server-waiting.md

File metadata and controls

50 lines (42 loc) · 1.33 KB

Server waiting

We wish to make the server wait until a player has joined before the pre-match countdown begins.

The first change is to track whether or not a player has joined yet (in the ShooterGameMode class) and update it when a player connects.

// Source/ShooterGame/Public/Online/ShooterGameMode.h

class AShooterGameMode : public AGameMode
{
 	// ...
	/** Tracks if a game has joined the server at least once */
	bool bHasPlayerConnected = false;
	// ...
}
// Source/ShooterGame/Public/Online/ShooterGameMode.cpp

void AShooterGameMode::PostLogin(APlayerController* NewPlayer)
{
	Super::PostLogin(NewPlayer);

	bHasPlayerConnected = true;
	// ...
}

Lastly, we can use this variable in the timer, to specify whether to decrement the timer, or not.

// Source/ShooterGame/Public/Online/ShooterGameMode.cpp

void AShooterGameMode::DefaultTimer()
{
    // ...
    if (MyGameState && MyGameState->RemainingTime > 0 && !MyGameState->bTimerPaused)
	{
		// If we are waiting for match to start (MatchState::WaitingToStart) and there has not yet been a connected
		// player, do not decrement the counter
		if (GetMatchState() == MatchState::WaitingToStart && !bHasPlayerConnected)
		{
			return;
		}

		MyGameState->RemainingTime--;
    // ...
    }
    // ...
}