546 Game Development and Simulation with Unreal Technology
FIGURE 8.44: Only Update Score if Active Ball Spawner E vent.
18. Next, we w ill nee d to make sure that each B all Spawner keeps trac k of the gam e
state. To do this, we will communicate this information from the level blueprint
by calling each spawner’s GameState event. To do this, perform the following
tasks:
a. Return to the level blueprint. Open it up if it is closed.
b. Find the Set function f or the GameIsA ctive variable.
c. Drag a wire off from this Set function.
d. Search for and place a F or Each Loop.
e. Drag a Get for the array of BallSpawners to the left of the For Each
Loop. Note: You can Control+Drag the BallSpawners array from the
MyBlueprint to place a getter for it.
f. Connect the output of the BallSpawners to the Ar ray input of the For
Each Loop.
g. Drag a wire from the Arr ay Element and place a Cast To BallSpawner
function.
h. Make sure that the exec(white) output of the For Each Loop is also con-
nected to the input exec(white) pin of the Cast to BallSpawner function.
i. Drag a wire off from the As Ball Spawner output pin of the ca st fu nction
and place a Game State function call.
j. Make sure that the IsGame Active checkbox is unchecked.
19. Your network should look similar to Figure 8.45.
FIGURE 8.45: Update BallSpawners Game Active Variable.
20. This ma kes sure that when the for-each loop is called, we will tell each
BallSpawner in our level that the game has ended and we do not want to add
any more points to our score.
21. Compile and Save the bluep rint.
Advanced Blueprint Concepts 547
22. Give your game a try to make sure that the logic is implemented properly and
that w hen the game ends, no more objects are spawned, and the player’s score no
longer updates.
ESTABLISHING THE GAME SAVE/LOAD FUNCTION ALITY
With our game state working, we can set up our saving and load ing system. We
will create a blueprint based on the SaveGame class. A Sav eGame blueprint is a
special blueprint used only for saving and loading game data.
23. Navigate to th e Content Browser and go to th e Blueprints folder.
24. Create a new blueprint.
25. The parent class we will be using this time is the SaveGame class. Search for this
at the bottom a nd hit select.
26. Give your new blueprint the name MySaveGame and op en it.
As you can see, you cannot put physical comp onents into this blue print. This
blueprint should only con ta in data that you want to have saved an d loaded in
your game. In our case, the only piece of da ta we will be saving is the player’s
high score.
27. Create an Integer variable called High Score.
FIGURE 8.46: Creating the High Score Variable.
28. In the My Bluepr int pane, there should be an icon that looks like a closed eye
next to the variable.
29. Left-click on this icon so that it looks like an open eye. This will make the
variable public so we can access it using get an d set functions. Note that if the eye
is yellow, it warns you that there is no Tooltip included for this variable.
30. Go to the Details rollou t and add a Tooltip that reads High Score for t he
game. This will g et rid of the warning and the eye will have a green background.
31. Compile, save, and close the MySaveGame blueprint.
548 Game Development and Simulation with Unreal Technology
32. That is all we need to do to set up the MySaveGame blueprint at this point.
33. Now we need to save and load the game data in the Level Blueprint.
a. Open up the level blueprin t.
b. Locate the Setup Array logic (this is where we inclu ded each individual
Ball Spawner into the BallSpawners A rray).
c. We will need to use the Event Begin Play to load the game on star tup.
d. Create a Sequence nod e above the Setup Array network.
e. Disconnect the Event Begin P lay from the Set of the BallSpawners by
Alt+Clicking on either end of the connection.
f. Connect the exec(white) output of the Event Begin Play function to the
exec(white) input o f the Sequence node.
g. Attach the Then 1 pin to the existing Set Ba ll Spawners function.
h. Your network should loo k similar to Figure 8.47 so far.
FIGURE 8.47: Placing the Sequence for the Game Load/Save.
34. To make things easier, we can create a variable to store the name of the save file,
to make sure we do not a ccidently name it incorrectly in another location.
a. Create a String variable called SaveName.
b. Set the default value of this variable to Level01Save File in its Details
rollout.
c. We w ill also need a variable to temporarily sto re a reference to our save
game data.
d. Create a variable called SaveGameLocal and set its variable type to Save
Game.
e. Lastly, we need to store our current high score in a variable as well.
f. Create an integer variable and name it CurrentHighScore.
g. The default value for this variable should be left at zero.
35. The first thing we need to check is if the Save Game file exists in the default save
location.
a. Drag a wire off from the Then 0 and place a function called Does Save
Game Exist to the Then 0 pin on the Sequenc e node.
Advanced Blueprint Concepts 549
b. Place a Get for the Save Name variable we created ea rlier to the left of this
function. Note: You can Control+Drag the SaveName variable to place a
getter.
c. Connect the Slot Name pin to the Save Name variable we just created. This
will check for a save file called Level01SaveFile and return true or false
on its R eturn Value pin depending on whether or not it exists.
d. Your network should look similar to Figure 8.48.
FIGURE 8.48: Check Whether the Save File Exists.
36. Now we will cre ate two networks for either case, if the save file exists or if it does
not exist. If the save gam e file does no t exist, we must create it using a Create
Save Game Object function by performing th e following steps:
a. Drag a wire off fr om the Return Value of the Does Save Game Exist
node and place a Branch stateme nt.
b. Make sure that the input of the Branc h and the Does Save File Exist
are connected.
c. Drag a wire from the False output of the Branch and place a Create Save
Game Object.
d. For the Save Game Class of the Create Save Game Object select the
dropdown menu.
e. From the menu choose My Save Game.
f. Place a Set for the Sa veGameLocal variable to the right of the
CreateSaveGameObject function. You may Alt+drag the variable to place
a setter function.
g. Connect both the exec(white) and object(blue) pins of the Set and the
CreateSaveGameObject.
h. Drag a getter function for the SaveGameLocal below the Set function. You
may Control+Drag the variable to place a getter.
i. Drag a wire off from the Get SaveGameLocal, search for and place a Cast
To MySaveG ame function.
j. Connect the SaveGameLocal to the Object of the Cast To node, if it is
not already connected.
k. Drag a wire off from the Cast To function , search for and p la ce a
SaveGametoSlot functio n.
550 Game Development and Simulation with Unreal Technology
l. Make sure that the As My Sa ve Game is connected to the
SaveGameObject.
m. Drag a getter function for the SaveName and connect it to the Slot Na me
of the SaveGametoSlot function.
n. Your network should loo k similar to Figure 8.49.
FIGURE 8.49: Save Game into File and Store Name if It Does Not Exist.
37. This will create our save file if one does no t already exist. Next, we need to add
the logic, to handle the save file if it already exists:
a. Drag a wire off from the True pin of the Branch node.
b. Add a Load Game f rom Slot function.
c. Again, create a Get for the Save Name variable and place it to the left of
the Slot Name.
d. Connect the Get to the Save Name pin of the Load Game from Slot func-
tion. Leave the user index at zero.
e. Next, place a Set function for the Save Game Local variable and connect
its input pin to the Return Value o f the load function .
f. Connect the exec(white) output pin of the Load Game from Sl ot to the
input exec(white) p in of the Set.
g. Place a Get for the SaveGameLocal variable below the Set.
h. Drag a wire off from this Get to add another Cast To MySaveGame func-
tion to cast it to a MySaveGame variable.
i. Drag from the As My Save Game and place a Get High Score.
j. Add a Set function for the CurrentHighScore variable to the right of th is
network and feed it the High Score variable.
38. Your network should look similar to Figure 8.50.
FIGURE 8.50: Load if Save Game File Exists.
..................Content has been hidden....................

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