quarta-feira, 18 de junho de 2014

GameHUD: Initializing the variables with a Macro

The sample game has a Blueprint called GameHUD which draws the game information on the screen. To keep the game simple, this Blueprint will also be responsible for various rules of the game.

The construction of this "Blueprint" will be split into several articles. In this article we will create the Blueprint GameHUD in the editor, define the variables and add a Macro that will initialize the variables of the game.

In the top menu of the editor, click the "Blueprints" button and select the "New Class Blueprint" option:


On the next screen we need to choose the class that will be the basis for GameHUD. We will use the "HUD" class as a basis so we can draw on the screen. But the "HUD" class is not one of the 5 standards classes that the editor shows. To find the "HUD" class expand the "Custom Classes" option and type "hud" in the search box. Choose the "HUD" class and click the "Select" button as the image below.


On the next screen select the "Blueprints" folder and put the name of the blueprint as "GameHUD":


The blueprint GameHUD must contain the following variables:
  • Level (integer type): The current level of the game.
  • Score (integer type): The player's score.
  • StatueCont (integer type): keeps the amount of statues that were collected.
  • Time (integer type): Time remaining to end the game. 
  • GameOver (boolean type): Variable that indicates if the game ended.

Switch to the "Graph" mode and create the variables below, all with the "Editable" option selected:


In the image above we can see a Macro named "StartGame". This Macro is responsible for initializing the variables of the game.

The use of a Macro allows the definition of a set of actions that can be called in other parts of the EventGraph. That way we avoid repeating this set of actions in the EventGraph simplifying its modification by being in one place.

In this sample game we need to initialize the variables that control the game on two occasions. The first is when the game starts running. The second time is after the end of a game, when the player wants to try again.

To create this Macro click the "Macro" button that is in the "My Blueprint" tab of the "Graph" mode and put the name "StartGame":


In the "Details" tab you can specify the input and output parameters. As input parameter we will put just one line of Execution. To do this expand the "Inputs" category and click the "New" button. Put the name as "In" and type as "exec". Do the same in the "Outputs" category, but put the name as "Out", as the image below.



The following image shows the "StartGame" Macro with the input and output parameters. 


Now hold the "Alt" key and drag the five variables we created earlier to the Graph of the "StartGame" Macro. This will create "Set" actions for the variables. Set the following initial values ​​for the variables:
  • Score: 0
  • Level: 1
  • StatueCont: 0
  • Time: 30
  • GameOver: False (unchecked)

Organize the actions within the Macro "StarGame" to become as follows:

Click to enlarge

The last action is a call to "SetTimer" function with the following parameters:
  • Function Name : Clock
  • Time : 1
  • Looping : True (checked)

The use of the "SetTimer" function will be explained in the next article.

The image below shows the "StartGame" Macro being called in the "BeginPlay" event. With this call the actions that are within the Macro are executed.



Next: GameHUD: Timer and Custom Event
Prev: Initial preparation of the game
Table of Contents