We can create new Functions in Blueprints. Just as Macros, functions allow a set of actions that are executed in various parts of the Blueprint to be gathered in one place for easy organization and maintenance of the script. An advantage of Functions over Macros is that they can be called from other Blueprints. Compared to Custom Events, functions have the advantage of having return values.
As an example, let's create a function called "StatueCollected" in the "GameHUD" Blueprint. This function will be called by the "Statue" Blueprint when the player get a statue. The function should do the following actions:
As an example, let's create a function called "StatueCollected" in the "GameHUD" Blueprint. This function will be called by the "Statue" Blueprint when the player get a statue. The function should do the following actions:
- Add to the player's score, the points obtained when collecting a statue which is calculated with the expression "10 X Level".
- Add 1 to the "StatueCount" variable that stores the amount of statues that were collected.
- Test whether the value of the "StatueCount" variable is multiple of 5. If true do the following actions:
- Add 15 to the "Time" variable.
- Add 1 to the "Level" variable. Limit the maximum value of the level in 5.
Every 5 statues collected the player increases a level and get 15 seconds of time.
To create the function, open the GameHUD blueprint and go to the "Graph" mode. In the "MyBlueprint" tab click the "Function" button as the image below.
To create the function, open the GameHUD blueprint and go to the "Graph" mode. In the "MyBlueprint" tab click the "Function" button as the image below.
Put "StatueCollected" as the function name. To edit it, double-click the name of the function.
I will show separately the actions that are part of this function. In the end I'll show an image containing the complete function.
As the above list, the first action that should be done is:
I will show separately the actions that are part of this function. In the end I'll show an image containing the complete function.
As the above list, the first action that should be done is:
- Add to the player's score, the points obtained when collecting a statue which is calculated with the expression "10 X Level".
To calculate the new points, you must use arithmetic operators and expressions. The variables that are used in the calculation were created in the article "GameHUD: Initializing the variables with a Macro".
The first part of the function will be as follows:
The image above shows that the new value of the "Score" variable will be obtained from its old value plus the result of the operation "Level x 10".
The next action to be done is:
The next action to be done is:
- Add 1 to the "StatueCount" variable that stores the amount of statues that were collected.
This action is very simple:
The next action:
- Test whether the value of the "StatueCount" variable is multiple of 5.
For this test we will need to use relational operators and branch. This test will also use the "%" operator known as Module. This operator returns the remainder of a division. To find this operator simply right click in the EventGraph and search for "%".
The Branch will look like this:
Every 5 statues collected the player increases in level. To check if the variable "StatueCount" is multiple of 5 simply use the expression "StatueCount % 5" and verify if the result is equal to 0. If the result of the expression is true it means that the player increased level and then we will perform the next two actions.
- Add 15 to the "Time" variable.
This action gives the player an additional 15 seconds of play:
- Add 1 to the "Level" variable. Limit the maximum value of the level in 5.
This action increases the player's level. The "MIN" operator was used, which returns as a result the lowest input value, so that the level value was never greater than 5:
With that we complete the "StatueCollected" function. The full version of the function can be seen in the image below.
Next: GameHUD: Drawing on the screen
Prev: GameHUD: Timer and Custom Event
Table of Contents