A simple game in Unreal C++
In Part I articles we will program a game to see the concepts of Unreal C++ in practice. It will be the same game of collecting statues that I created for my Blueprints tutorials, so you can compare the implementations made in Blueprints and C++.
The game is based on the Third Person template from Unreal Engine. There are some modifications I made to the game for the C++ implementation, but the end result of the game will be similar to the Blueprint version. This is an image of the game:
The rules of the game are as follows:
- The player must collect the items that appear in the scene before time runs out.
- The initial time is set at 30 seconds.
- The game is over when the time runs out.
- For every five items collected, the Player Level increases, and 15 seconds are added to the time.
- The player starts at Player Level 1, and the maximum level a player can reach is Player Level 5.
- The items in the scene change location periodically.
- When an item appears in a location, it will stay there for a period of time that depends on the current Player Level. The number of seconds it takes an item to change location is the result of the expression 6 – Player Level. For example, if the Player Level is 2 then the item will stay in a location for 4 seconds (6 – 2).
- When the player gets an item, another one is spawned in a random location.
- The score for an item collected is determined by the expression 10 x Player Level. For example, at Player Level 3, each item will be worth 30 points (10 X 3).
- The time, score, and Player Level values will be drawn on the screen.
The game uses four C++ classes which are:
- TutoProjectGameMode: This class controls the state of the game and stores some variables, such as those used for time, score, and Player Level.
- TutoProjectCollectable: An Actor class that represents the item that can be collected by the player. It checks for collision with the player and has the logic to change its location periodically.
- TutoProjectCharacter: Class that represents the player and is part of the Third Person template.
- TutoProjectHUD: Class responsible for drawing the time, score, and Player Level values on the screen.
In addition to the C++ classes, a Blueprint will be created using the TutoProjectCollectable C++ class as the parent class.
The game was made using Unreal Engine version 4.25. 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.