Saving and loading game progress

The last thing that we will focus on is saving and loading game progress. Saving and loading can be done in many ways, but at its heart, saving the progress revolves around specific variables that you would like to save.

Saving

Most games save a lot of different variables, such as the level or area the player is in, the player's stats, the player's inventory, and gold. In our example, we will choose to save the player's gold, but using the method that we are about to perform, you can easily figure out how to save all the other progress in the game.

To start with, create a new Blueprint class in Content Browser by navigating to Content | Blueprints. The Pick Parent Class window will pop up, and from All Classes, select SaveGame:

Saving

Name this class NewSaveGame, and open the class. The purpose of this class is to hold the values of every variable that you would like to save. As mentioned earlier, for this example, we will be saving the gold variable, but if you would like to save more variables, the NewSaveGame class that you just created will store those variables as well. At this point, add a new variable to this class from the Add New variable in the My Blueprint tab. Name it Gold, and make its variable an Integer type:

Saving

Now that you are done, it is time to find a good spot for the game to be saved and the gold variable to be saved. Since we already have a pause menu and learned how to add buttons to the pause menu in the previous chapters, it would be easy to create a new button that we can call Save in the Pause_Main Widget Blueprint and add an OnClicked event to it:

Saving

Once you click on + next to the OnClicked event, the Event Graph will open up, and you will see the OnClicked event for your Save button. Here, allow the button, when pressed, to create a save game object. To do this, create Create Save Game Object whose Save Game Class is set to New Save Game, and allow it to start when you click on the button:

Saving

Here, we will need to create a new variable of the Save Game type, and we will call this variable save:

Saving

Here, we will create a SET Save variable in our Blueprint, and pass the Return Value of the Create Save Game Object function to SET Save:

Saving

We will now need to cast the NewSaveGame class so that we can set the Gold variable that we created to the game's gold. To do this, cast SET Save to NewSaveGame by connecting the Save value to the Object of Cast To NewSaveGame:

Saving

Next, allow Cast To NewSaveGame to fire off a Cast To RPGInstance whose Object is a reference to Get Game Instance. We are doing this so that we can get an instance of the GameGold, so link the As RPGGame Instance pin from Cast To RPGGameInstance to the Get GameGold variable from the RPG instance:

Saving

Now that we are getting the game gold, we can set the game gold's value to the Gold variable from the NewSaveGame class by linking the SET Gold to fire off when the RPGGameInstance is cast, and then linking the GameGold value pin to the Gold value pin and the Target pin of SET Gold to the As New Save Game pin from Cast To NewSaveGame:

Saving

This particular method will allow us to save whatever the current game gold is to our Gold variable in the NewSaveGame class. Note that if you want to save more variables, set the values of those variables just like you set the value of the Gold variable by adding a SET node for each individual variable that you have.

The last thing we will need to do is create a save game slot, which will hold our save game object. To do this, create a Save Game to Slot action, which you will find under Game in your Actions window:

Saving

Create a slot name for this; in this example, we will use A as Slot Name. Link the Save Game Object pin of Save Game to Slot to the Save value pin, and allow the Save Game to Slot to fire when the Gold variable is set:

Saving

We are now done with the saving part of the game. We will now move on to loading a game slot.

Loading

Just like saving, loading can be done in a number of ways. In our game, we will simply load the player's save data on launching the game. To do this, open the FieldPlayer Blueprint since we know that the FieldPlayer will always exist in our game.

Next, we will create a Load variable of the Save Game type similar to what we did when we saved the game, so that we can properly cast the variables and their values from NewSaveGame:

Loading

Here, we will create an Event Begin Play, and from Event Begin Play, we will call the Does Save Game Exist function from the Game category in the Actions window, and under Slot Name, we will look for A since we named our save slot A in the previous section:

Loading

From Does Save Game Exist, we will call a branch; if Does Save Game Exist is true, we will call Load Game from Slot and its Slot Name will also be A:

Loading

At this point, we have created a logic where, when the game starts, we check whether the saved game in slot A exists. If it does exist, we load the game from slot A; if it does not exist, we do nothing.

We can now set the Load variable that we created in the beginning of this section whose data type is Save Game to the Return Value of Load Game from Slot and cast it to NewSaveGame similar to what we did with our save game data:

Loading

Note that since we now have access to all the variables in NewSaveGame, it means that we have access to the gold value that we saved. So from here, we get the gold value from Cast To NewSaveGame so that you have whatever value was stored in Gold since the player last saved, and you will need to set the GameGold value from RPGGameInstance to Gold from NewSaveGame:

Loading

Just like when we created the saving logic, in this loading logic, if there are any other variables you need to load, you can easily do so by getting more variables from the NewSaveGame class and setting it to other variables from the RPG Game Instance.

You can now test this by simply playing the game, doing battle to get the game gold, saving the game with the Save button that we created in the pause menu, and then closing the game. When you reopen the game, you will notice that the gold you had when you saved the game is automatically loaded when you start the game. Using this framework, feel free to save other game variables such as your status and inventory

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

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