TutoProjectGameMode class: Get functions and the new HUD class

We created the game state variables in the ATutoProjectGameMode class with the protected access modifier to prevent other C++ classes from directly modifying the values of these variables.

However, the ATutoProjectHUD class needs to have access to some of these variables in order to be able to draw their values on the screen. So, let’s create some public Get functions that return the values of these variables.

The variables that need to be accessed by the ATutoProjectHUD class are PlayerLevel, Score, Time, and bGameOver.

Open the file TutoProjectGameMode.h and add the function declarations from the code below that are after void ItemCollected().


	void StartGame();

	void ItemCollected();

	int32 GetPlayerLevel();

	int32 GetScore();

	int32 GetTime();

	bool  IsGameOver();

The int32 type before the function name indicates that the function returns an integer value. In the case of Boolean variables (true/false), the convention for the Get function is to use Is plus the variable name as in IsGameOver().

The Get functions are very simple, they just return the values of the variables. Open the TutoProjectGameMode.cpp file and add the function definitions at the end of the file as shown in the code below.

int32 ATutoProjectGameMode::GetPlayerLevel()
	return PlayerLevel;

int32 ATutoProjectGameMode::GetScore()
	return Score;

int32 ATutoProjectGameMode::GetTime()
	return Time;

bool ATutoProjectGameMode::IsGameOver()
	return bGameOver;

If you want other classes to modify the value of a protected or private variable, you can create a public Set function. We will not need Set functions in the ATutoProjectGameMode class, but below is an example of a Set function that modifies the Score variable.

void ATutoProjectGameMode::SetScore(int32 NewScore)
	Score = NewScore;

One of the advantages of using a Set function is that you can validate the new value before storing it in the variable.

Another change that needs to be done to the ATutoProjectGameMode class is to define that the ATutoProjectHUD class will be used as the game’s HUD. This is done in the TutoProjectGameMode.cpp file within the ATutoProjectGameMode() constructor.

It is necessary to add the #include “TutoProjectHUD.h” so that the ATutoProjectGameMode class finds the definition of the ATutoProjectHUD class. The assignment of the new HUD class is done with this line:

HUDClass = ATutoProjectHUD::StaticClass();

The StaticClass() function is a static function that returns a reference to the class. Static functions belong to the class, that is, they do not need an instance to be executed.

The beginning of the TutoProjectGameMode.cpp file looks like this:

#include "TutoProjectGameMode.h"
#include "TutoProjectCharacter.h"
#include "UObject/ConstructorHelpers.h"
#include "TutoProjectHUD.h"

  // set default pawn class to our Blueprinted character
  static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(
  if (PlayerPawnBPClass.Class != NULL)
    DefaultPawnClass = PlayerPawnBPClass.Class;

  HUDClass = ATutoProjectHUD::StaticClass();


The assignment of DefaultPawnClass is different from that of HUDClass because it is being used a Blueprint called ThirdPersonCharacter that was created by the Third Person template. The parent class of this Blueprint is TutoProjectCharacter.

If these Game Mode class assignments were being done in Blueprints, they would be done in the Class Defaults tab:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed