segunda-feira, 3 de agosto de 2020

Game state variables

The state of our game will be maintained through values that will be stored in variables. These variables will be created in the TutoProjectGameMode.h file, because one of the objectives of a GameMode class is to keep the rules of the game.

There are variables of different types. Let's create the following Integer variables:
  • PlayerLevel: Stores the player's current level.
  • Score: Stores the player's score.
  • ItemCount: Stores the number of items collected.
  • Time: Stores the time remaining until the end of the game.

There is a type of variable called Boolean. A variable of this type can only store the values true or false. Let's use the following Boolean variable:
  • bGameOver: Indicates if the game has ended.

The letter b at the beginning of a variable's name is a convention used to indicate that the variable is Boolean.

The variables will be created with the protected access modifier, to prevent other C++ classes from directly modifying the values of these variables.

The file TutoProjectGameMode.h with the definition of the variables looked like this:
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "TutoProjectGameMode.generated.h"

UCLASS(minimalapi)
class ATutoProjectGameMode : public AGameModeBase
{
	GENERATED_BODY()

public:
	ATutoProjectGameMode();

protected:
	int32 PlayerLevel;

	int32 Score;

	int32 ItemCount;

	int32 Time;

	bool  bGameOver;
};

The int32 keyword defines a 32-bit integer variable. Such a variable can store values in the range of -2,147,483,648 to 2,147,483,647.

The bool keyword defines a variable of type Boolean. Note that all variable definitions end with the character ;.