Pages

terça-feira, 11 de agosto de 2020

Creating the ItemCollected function in C++

In the previous article, we saw the basic concepts of operators and if conditional. In this article, we will see these concepts in practice when implementing the ItemCollected() function in the ATutoProjectGameMode C++ class.

This function will be called by the TutoProjectCollectable class when the player collects an item in the game. In our game, an item is represented visually by a statue, but we will see in another article how to change its appearance.

The
ItemCollected() function must do the following actions:
  • Adds to the value of the Score variable the points obtained when the player collects an item, which is calculated with the expression 10 x PlayerLevel. 
  • Adds a value of 1 to the ItemCount variable, which stores the number of items collected.
  • Tests whether the value of the ItemCount variable is a multiple of 5. If true, it performs the following actions:
    • Adds a value of 15 to the Time variable.
    • Adds a value of 1 to the PlayerLevel variable, with the maximum value of the PlayerLevel variable limited to 5

For every 5 items collected the player increases a level and gains 15 seconds of time.

The declaration of the ItemCollected() function is done in the header file TutoProjectGameMode.h below the declaration of the function StartGame() as shown in the code snippet below. The ItemCollected() function is public because it will be called by another C++ class. 
...

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

public:
	ATutoProjectGameMode();

	void StartGame();

	void ItemCollected();

...
I use an ellipsis ... to indicate that the source code displayed is not complete.

The body of the ItemCollected() function must be placed in the TutoProjectGameMode.cpp file below the BeginPlay() function:
...

void ATutoProjectGameMode::BeginPlay()
{
	Super::BeginPlay();

	StartGame();
}

void ATutoProjectGameMode::ItemCollected()
{
	Score += PlayerLevel * 10;
	ItemCount++;

	if (ItemCount % 5 == 0)
	{
		Time += 15;

		if (PlayerLevel < 5)
		{
			PlayerLevel++;
		}
	}
}

Let's look at each of the actions done by the ItemCollected() function along with the equivalent source code.

  • Adds to the value of the Score variable the points obtained when the player collects an item, which is calculated with the expression 10 x PlayerLevel
Score += PlayerLevel * 10;
I'm using the shorthand operator += which is an arithmetic operator together with the assignment operator. The += operator adds the result of the expression on the right to the current value of the Score variable.

One of the advantages of the C++ language in relation to Blueprints is the writing of expressions. Complex expressions can be written on a single line of C++ code, whereas in Blueprints the same expression will need many nodes to be written.
 
  • Adds a value of 1 to the ItemCount variable, which stores the number of items collected.
ItemCount++;
I am using the increment operator ++ which adds 1 to the current value of the ItemCount variable.

  • Tests whether the value of the ItemCount variable is a multiple of 5. If true, it performs the actions below.
if (ItemCount % 5 == 0)
The block of code below the if conditional that is enclosed in curly braces { } will only be executed if the result of the expression inside the parentheses is true. In the expression, I am using the % operator that returns the remainder of the division. The % operator is known as Modulo. When ItemCount has a value that is a multiple of 5, the remainder of the division by 5 is 0. The % operator takes precedence over the == operator, so the remainder of the division is done before the comparison with 0.

    • Adds a value of 15 to the Time variable.
Time += 15;
This line gives the player another 15 seconds of playtime.

    • Adds a value of 1 to the PlayerLevel variable, with the maximum value of the PlayerLevel variable limited to 5.
if (PlayerLevel < 5)
{
	PlayerLevel++;
}
I'm using the if conditional to prevent the PlayerLevel from getting higher than 5.