Storing and loading the saved data when starting the game

Now that we have a container for our saved data, we need to ensure that the data is stored somewhere on the player's machine and that it is retrieved when the player returns to the game. We also want the saved data to be updated each time the level loads because we will increase the current round number each time the player wins a round. Like the rest of our gameplay settings, we will add this process to the FirstPersonCharacter Blueprint. Now, go to Content Browser, open the Blueprints folder, and open FirstPersonCharacter.

In addition to the SaveSystem Blueprint, which will store information about what gameplay data to save, we are going to need Save Game Object that will actually contain the particular data for that user. To easily reference this saved data, we will save it in a variable that we can reference throughout FirstPersonCharacter. From the My Blueprint panel, create a new variable called SaveGameInstance. In the Details panel, click on the Variable Type drop-down menu and search for Save System. Select the Save System option to allow this variable to contain an instance of the SaveSystem Blueprint that we just created, as shown in the following screenshot. This variable does not need to be editable, and you should leave the default value to None:

Now, find the block of Blueprint nodes that draws the Heads-Up Display (HUD) on the screen when the gameplay begins, triggering the Event BeginPlay node. Similar to what we did with the WinMenu sequence, break the connections between Event BeginPlay and Create HUD Widget. Then, drag the Widget node and its connections to the side to make room for a significant number of new nodes. Back at the Event BeginPlay node, drag a wire out from its output execution pin, and then attach it to a Does Save Game Exist node.

When the game begins, we are going to use Does Save Game Exist to check for the existence of a save game file that features the save slot and user we specify in the node. We are going to save our data in a single save slot only so that each save operation will overwrite the previously saved data. Additionally, we will not create a user system for our game, so anyone playing the game on a particular machine will be assumed to be the only player. A Branch node will direct the game operations, depending on whether or not a saved game is found. If no saved game exists, then a new Save Game Object of SaveSystem type will be created. If one already exists, then we will load the saved data from it.

The nodes used to accomplish this can be seen in this screenshot:

First, we must determine what our saved game slot is going to be called. In the Slot Name input field of Does Save Game Exist, type BlueprintGameSave. Leave the User Index input at 0. This combination asks the player whether there is a saved game by the first user in the user index, which will be our only user, and in the save slot called BlueprintGameSave, which will be our only save slot. Next, drag a wire from the Return Value output pin of Does Save Game Exist, and then attach it to a Branch node.

Whenever you are constructing a complex system, such as our save structure, it is wise to use the Print String nodes, as shown in the previous screenshot, to evaluate whether your Blueprint logic is behaving the way you think it should during gameplay. In the preceding case, we are finding out whether or not a saved game was found and printing the result.

From the Branch node, we will create a path to load content from a saved game, and another path to create a save game file. Drag a wire from the True output execution pin of the Branch node. If you wish to see a debug message printed on the screen when a saved game is supposed to be loaded, then you can first route the True path of the Branch node through a Print String node, as seen in the preceding example. But this is not necessary to make the save system work. Whether you choose to use a Print String node or not, ensure that the Branch node is connected to a Load Game from Slot node, and then enter BlueprintGameSave in the Slot Name input field.

Next, drag a wire from the False output execution pin of the Branch node. Again, you have the choice of attaching the wire to a Print String node to aid in debugging or just attaching it directly to a Create Save Game Object node. Within this node, click on the Save Game Class input dropdown, and then select Save System from the options presented.

Now, we need to ensure that the saved data, whether we just created it or are loading it from an existing file, is stored inside our SaveGameInstance variable. This can be accomplished through casting, as seen here:

Start by dragging a wire from the Return Value output pin of the Load Game from Slot node, and then attach it to a Cast To SaveSystem node. Also, connect the execution pins of these two nodes. Then, drag the SaveGameInstance variable onto the As Save System output pin of the casting node to both create and attach a SET Save Game Instance node.

Next, attach the same variable data-storing nodes in the branch where we are creating a new Save Game Object. Drag the SaveGameInstance variable onto the Return Value output pin of the Create Save Game Object node. The cast node is not needed because the Create Save Game Object node returns an instance of the Save System class used as an input parameter.

With our Save Game Object created and stored in a variable, we need to save this data in a file that will be stored on the player's machine. Drag a wire from the output pin of the SET Save Game Instance node we just created, and then drop it onto a Save Game to Slot node. Inside this node, enter BlueprintGameSave in the Slot Name input field. This will complete the steps necessary to create or load a save file when the game starts playing. Select all these nodes and wrap them in a comment box in order to leave a note about their functionality for yourself.

..................Content has been hidden....................

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