The way that Game Over is implemented in this game is quite simple. When the time is up, the player can continue to move but he is unable to pick up the items, and the texts GAME OVER and Press Enter are displayed on the screen.
We have already implemented all the necessary logic for Game Over in the other articles. The only missing part is to draw the messages on the screen. I will mention the articles that have some logic related to Game Over.
The state of Game Over is controlled by a Boolean variable called bGameOver that was created in the article Game state variables.
The only condition that leads to the Game Over is when the time is equal to zero. This test is performed in the Clock() function that was shown in the article Creating a Timer in Unreal C++.
When the value of the variable bGameOver is true, the player is unable to pick up the items, because one of the conditions tested when overlapping an item is that the variable bGameOver cannot be true, as was seen in the article TutoProjectCollectable class: Functions.
The article TutoProjectHUD class: Drawing on the screen has a function called DrawHUD() that is used to draw the game information on the screen. In this function, we will add a conditional to test if it is in the Game Over state and draw the texts GAME OVER and Press Enter on the screen. The full version of the DrawHUD() function will look like this:
void ATutoProjectHUD::DrawHUD()
{
Super::DrawHUD();
FString textoHUD = FString::Printf(TEXT("Score: %d"), TutoProjectGameMode->GetScore());
DrawText(textoHUD, FColor::Yellow, 10, 10, nullptr, 3.0f, false);
textoHUD = FString::Printf(TEXT("Time: %d"), TutoProjectGameMode->GetTime());
DrawText(textoHUD, FColor::Red, 300, 10, nullptr, 3.0f, false);
textoHUD = FString::Printf(TEXT("Player Level: %d"), TutoProjectGameMode->GetPlayerLevel());
DrawText(textoHUD, FColor::Blue, 550, 10, nullptr, 3.0f, false);
if (TutoProjectGameMode->IsGameOver())
{
DrawText("GAME OVER", FColor::Red, 400, 200, nullptr, 3.0f, false);
DrawText("Press Enter", FColor::Red, 400, 400, nullptr, 3.0f, false);
}
}
It's done. We have completed the implementation of this simple game. Now just compile the project and try the game.
This article concludes part I of the Unreal C++ tutorials. The main objective of this part is to make you have the first contact with concepts of the C++ language in Unreal Engine and see them being used in practice.
The project with source code is available at this link:
When opening the project for the first time, a message will appear asking if you want to rebuild the modules. Click the Yes button.