quarta-feira, 8 de julho de 2015

Paper 2D Game: GameManager and GameMode

This article is the continuation of the article "Paper 2D Game: The cars".

In this article we will create a new Blueprint named "GameManager". This Blueprint is responsible for creating "EnemyCar", manage the score of the player and the current level of the game. It also writes on the screen the score and the current level.

Will also be made some adjustments in the "EnemyCar" Blueprint to interact with the "GameManager" Blueprint. At the end of the article will be created a new "GameMode" to use the "GameManager" Blueprint as the HUD. 

  • GameManager

Create a new blueprint of the "HUD" type. This requires expanding the "All Classes" option and search for "HUD":

Click to enlarge

Put the name "GameManager" for this Blueprint.

Create the variables: "Score" (Integer), "Level" (Integer), "EnemyCarSpeed" (Float) and "Menu" (Boolean).



The game can be on the Menu, which displays the message "Press ENTER to start" or running. This is controlled by the boolean variable "Menu". The BeginPlay event assigns a positive value for the variable "Menu", so the game starts in the menu. It also enables the Input events to the Blueprint "GameManager":


When pressing the Enter key, it is checked if the game is on "Menu", if true then the custom event "StartGame" is called:


The custom event "StartGame" prepares the variables and timers for a new game and creates an instance of "PlayerCar" in position ( X=320, Y=1, Z=100 ). The value of the Y axis is "1" so the "PlayerCar" stand in front of the background that is Y=0.

Click to enlarge

The "TimerCreateEnemyCar" event is initially called every 2 seconds. It just creates an instance of "EnemyCar" in position (X=320, Y=1, Z=520) which is above the game screen. A new variable was created in Blueprint "EnemyCar" that holds a reference to "GameManager". The creation of this variable is shown just below in the adjustments of "EnemyCar".


The "TimerAddScore" event is called every 0.5 seconds. It increases the points of the player according to the value of the variable "Level" multiplied by 50.


The "TimerChangeLevel" event is called every 10 seconds. These are some of the actions of this event:
  • Increases the enemy car's speed in 25.
  • Increases the value of the "Level" variable.
  • Recreates the "TimerCreateEnemyCar" with a lower time based on the current level.
  • Checks if is at level 5 to disable the "TimerChangeLevel", because 5 is the highest level.

Click to enlarge

The event "Receive Draw Hud" belongs to HUD class. It is used to draw the score and the level on the screen. The message "Press ENTER to Start" is drawn if the game is on the menu.

Click to enlarge

The custom event "Game Over" activates the menu, destroys all instances of "EnemyCar" and clears all timers. This event will be called by the "EnemyCar" when overlaps the player.

Click to enlarge


  • EnemyCar

We will complete the Blueprint "EnemyCar" that was started in the previous article. We must create a variable to reference the "GameManager". Check the "Editable" and "Expose on Spawn" so we can pass the reference to GameManager at the time of creation of the "EnemyCar" as seen above.


Change the "Tick" event so that it uses the variable "EnemyCarSpeed" of the "GameManager":

Click to enlarge

Create the "ActorBeginOverlap" event which tests the collision with the player and, if it occurs, notifies the "GameManager" that it's game over.

Click to enlarge

The use of the action "Cast To" above serves to check whether the Actor that overlapped is a "PlayerCar".

  • GameMode

Create a new blueprint of the "GameMode" type and put the name "GameMode2D". Double-click the blueprint and change the property "Default Pawn Class" to "Pawn" and in the "HUD Class" put the "GameManager": 



Access the "Edit->Project Settings..." menu, "Maps and Modes" category. In the "Default GameMode" select the "GameMode2D" we create:

Click to enlarge

With that we completed the implementation of "Paper 2D Game".


Next: 2D Animation with Flipbooks
Prev: Paper 2D Game: The cars
Table of Contents