terça-feira, 19 de agosto de 2014

Statue: Graph Actions

In this article we will create all the necessary actions for the "Statue" Blueprint.

The first thing we will do is create a variable to reference the "GameHUD" blueprint. Thus in the "Statue" blueprint we can read variables and call functions of the "GameHUD".

Switch to the "Graph" mode of the "Statue" blueprint and click the "+Variable" button. Put the variable name as "GameHUD". Click the combo box next to "variable type" to choose the type of the variable. Search for "GameHUD" and choose the "GameHUD_C" option that is in the "object" category. The "GameHUD_C" type was generated by the unreal editor when we created the "GameHUD" blueprint. The image shows the "GameHUD" variable:

In the "Begin Play" Event of the "Statue" Blueprint we need to store the reference of the GameHUD in the variable we created and then initialize the data of the "Statue".

Click to enlarge

To get the reference to the HUD being used by the game, we must first get a reference to the Player Controller. Then we use the "Get HUD" function passing the return of "Get Player Controller" function as the "target" parameter. 

The "Get HUD" function returns an object of HUD type. But we know we are using an object of GameHUD type, so we need to use a function such as "Cast to" to convert this result into a variable of "GameHUD" type. 

To add the action "Cast to GameHUD" click and drag the return value of the function "Get HUD" and release in the EventGraph to appear the list of available actions. The "Context Sensitive" option must be checked.

The actions needed to initialize the "Statue" were gathered in a custom event called "Init Statue":

Click to enlarge

In this event we have two main actions:
  • "Set Actor Location": This action sets a new location for the statue. This position is represented by a Vector (X, Y, Z) where the X and Y values are random. The minimum and maximum values of X and Y represent the area of the game. These values were obtained in the article "Initial preparation of the game".
  • "Set Timer": This Timer represents the time in seconds that the statue will take to change position. When this time expires, the event "Init Statue" is called to set a new position. The time is the result of the expression "6 - Level". The current level is obtained in the "Level" variable of the GameHUD blueprint. When you drag the result of the expression, which is an integer value, to the "Time" parameter that is fractional, the editor automatically creates a converter between the two types.

The last event of the "Statue" is the "Actor Begin Overlap" which is triggered when an Actor of the game is overlapping the statue:

Click to enlarge

The value passed to the "Condition" parameter of the Branch is the result of the boolean expression that checks if the actor who is overlapping is the player and the game is not in the "Game Over" mode. The statue will be collected only if these two conditions are true.

If the statue is collected, the "StatueCollected" function of the GameHUD is called to manage the player's score and the game level.

After that the "InitStatue" event is called to change the position of the statue.

These are all the necessary actions for the "Statue" Blueprint. To finish add 3 instances of the "Statue" Blueprint on the level in any position.

Next: GameHUD: Game Over
Prev: Statue: Components
Table of Contents