Advanced Blueprint Concepts 541
for score.
36. Your network for showing the score and time on th e HUD should look similar to
Figure 8.38.
IMPLEMENTING THE E VENT DISPATCHER CALL FOR
GAME TIMER
We need to ca ll the UpdateTimer_HU D event and give it th e GameTimer value.
37. Return to the level blueprint.
38. Look for the scr ipt we recently created to update the GameTimer off of Event
Tick.
39. Add a Get Player Con troller function below the Max function.
40. Drag a wire off of the Get Player Controller node’s Return Val ue and
type Get HUD. This w ill attach a Get HUD function to our Get Player
Controller.
41. Drag a wire off of the Return Value of th e Get H UD function and place a Cast
To MyNewHU D function .
42. Attach the exec(white) input of the cast function to the exec(white) output of the
set function so we can cast our HUD reference to the correct blueprint type.
43. Finally, drag a wire off the As My New HUD pin.
44. Add the Update Time r HUD Event function ca ll.
45. Drag a Get for the GameTimer variable to the left of th e Update Timer HUD
Event. Note: You can do this by control+dragging the GameTimer variable
from the MyBlueprint pane.
46. Connect the ou tput of the GameTimer variable to the Timer input of the Update
Timer HUD function.
47. Your network should look similar to Figure 8.39.
48. Compile and Save your blueprint.
FIGURE 8.39: Calling the HUD Update Timer Event.
49. After this is done, return to the main editor.
50. Play the game. You should see the Timer value and Score value on the screen
and th e numbers should be upd ating (see Figure 8.40 ).
542 Game Development and Simulation with Unreal Technology
FIGURE 8.40: HUD Updates the Time and Score.
What Happened in TUTORIAL 8.7. . .
We have successfully updated our HU D to include the current timer value, as well
as update the score value to c orrectly label our informatio n. The informatio n the
player sees is an important p art of the player’s experience with the game. T he
player would not know what was happening behind the scenes if it were not for
a well- designed HUD providing quick and current data.
8.5 SAVING AND LOADING GAME DATA
Most curr ent games o f this day and age have some kind of saving/loading system
built in, so you can keep your game progress, high scores, game settings, items, and
so on. This option can b e given to the user so they can save their progre ss manually,
or it can be done automatically by the game when you cross a chec kpoint or enter a
new area in the game.
For our game, we want to keep a record of the player’s high score in the game. This
high score will be saved automatically when the timer has run out, and it will load
on the startup of the level to be displayed to the player even af te r the game has been
closed and reopened. Once again, bluepr ints will make this task very easy to visua lize
and script out in a fast and efficient manner.
Advanced Blueprint Concepts 543
FIND ON THE WEBSITE
To find updates to this tutorial and u pdated instructions about its implementa-
tion on other UE 4 versions, please visit the books companion Website at:
http://www.RVRLAB.com/UE4Book/
TUTORIAL 8.8 Adding t he Score
In this tutor ia l we will establish our game start/end conditions. We will also
implement a save/load mechanism to allow for the game to retain th e highest
score by the player.
ESTABLISHING ACTIVE GAME STATE
The first thing we need to do is set up our game to start and end based on the
Game Timer we created in the previous tutor ia l. Once this is done, we will be
able to check if the c urrent high score has been surpassed at the e nd of the game,
and update it if necessary. When the game is loaded up f or the first time, we will
check if a save file exists, and load the data f rom this file to be displayed on the
HUD.
1. First, open up the level blueprin t.
2. We need to add a Boolean variable that we will use to d etermine if the game is
currently active or not. To do this, perform the following tasks:
a. Create this variable and give it the name IsGameActive.
b. With the IsGameActive selected, go to its Details window, set its default
value to True by checking the box.
3. For our purposes, we will define an ac tive game state to be a ny state where the
GameTimer is not equal to zero. We simply n eed to check the GameTimer variable
to see if it is equal to zero, then act accordingly. Perform the following tasks:
a. Find the Timer Logic network that we created in the p revious tutorial.
b. Drag a wire off from the Update Timer HUD Event function c all, and add
a Branch statement.
c. Pull a wire off from the GameTimer variable and search for a == (two equal
signs).
d. Connect the ou tput of the == to the Condition in put of the Bran ch variable.
This will check if this variable is equal to the value 0.0.
e. Drag a Set for the IsG ameActive variable to the right of the Branch state-
ment.
f. Connect the True outp ut of the Branch to the exec(white) input of the Set
for the I sGameActive variable. Make sure that the checkbox next to the
IsGameActive is unchecked. This will set our IsGameActive to False (see
Figure 8.42).
544 Game Development and Simulation with Unreal Technology
FIGURE 8.41: Establishing Game Active State.
ENABLING SPAWN BEHAVIOR ON ACTIVE GAME STATE
Now that we have out Active Ga me State setup, we need to add a Branch state-
ment to the spawn logic to prevent new balls from spawning once the game has
ended.
4. Inside the Timer logic, find the Then 1 pin on the Sequence function that we
connected to Set function for updating the sp awn timer.
5. Disconnect the T hen 1 f rom the Set Spawn Timer by Alt+clicking on either
end of the co nnection.
6. Add a Branch in the now-open area between the Then 1 from the Set Spaw n
Timer.
7. Also, add a Get for the IsGameActive variable by Control+Drag ging the
IsGameActive variable to the left of the Branch node that you just adde d.
8. Connect the Is GameActive getter to the Condition of the Branch.
9. Connect the True o utput of the Branch to the exec(white) input of the Set node.
This will make sure that the ball will only spawn if the game is curre ntly active.
10. Your network should look similar to Figure 8.42.
FIGURE 8.42: Spawning Network for When the Game Is Active.
ESTABLISHING INACTIVE GAME STATE
At this point, the game will stop spawning balls once the timer hits zero. Next,
we have to tell th e BallSpawner c la ss that the game has become inactive. If we
Advanced Blueprint Concepts 545
do not do this, the player will be able to continue to score points after the time
has expired by picking up balls that a lready existed before the timer ran out.
11. First, go to the BallSpawner class and open it up.
12. We will now create a variable and an event which will set this variable as the state
of the g ame. To do this, perform the following tasks:
a. Right-click on the event gra ph to add a custom function called
GameState.
b. With the GameState custom event selected, look in the Details rollout and
create a Boolean input called Is Game Active. We will use this to pass in
a Boolean so tha t the BallSpawner is aware of the current game state.
c. Create a variable called IsGameActive in the BallSpawner class and a t-
tach it to the event.
d. Drag the IsGameActive variable and place a Set for it to the right of the
GameState. Note: You can place a setter by Alt+Dragging the variable
onto the event graph.
e. Connect the output IsGameAct ive of the GameState event to the
IsGameActive input of the Set.
13. Your network should look similar to Figure 8.43.
FIGURE 8.43: Setting Game State in the Ball Spawner Event.
14. Next, we need to make a condition to only update the character’s score if the game
is active. To do this, perform the following tasks:
a. Disconnect the Destroy Actor from the Set for the Is Spawn Active
variable.
b. Add a Branch statement between the Destroy Actor function and before
the Update Score Even t.
c. Place a Get for the new Boolean IsGameActive be low th e Destroy Actor
and to the lef t of the Branch.
d. Connect this getter to the Condition for the b ranch statement.
e. Connect the true output o f the Branch to the exec(white) input of the Set
Is Spawn Act ive that you just disconnected.
15. Your network should look similar to Figure 8.44.
16. Now, if the player continues to collect balls after the time has expired, the game
simply will not add them to the score.
17. Compile and Save your blueprint.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.117.212