segunda-feira, 12 de janeiro de 2015

Save and Load in Blueprints

To Save a game we need to define all the information that is necessary to store to be able to load the game later. This information needs to be gathered and stored in variables of a Blueprint of the type "SaveGame". The image below shows the functions used to Save and Load a game.

As an example of using these functions we will add the option of Quick Save and Quick Load to the game that we developed in the first part of this blog. The game is described in this article: "A very simple game in Blueprints".

But do not worry if you have not done the game of the first part, because the concepts presented in this article can be easily adapted to your game.

The first step is to create a new Blueprint of the type SaveGame. To do this search for "Save" in the option "All Classes" and choose as Parent Class the class "SaveGame" as shown in this image:

For my example, I chose the name "SaveInfo" for this new Blueprint that will contain all the information that will be saved.

We will save the variables that define the current state of the game. The variables are Level, Score, StatueCont and Time. These variables are of type Integer and are described in article: "GameHUD: Initializing the variables with a Macro".

Besides these variables, we will also save the Transformations of the Player and of all the "Statues" of the game. A variable of the type Transformation contains information about the Location, Rotation and Scale of an Actor. Thus when loading a game we can put the Player and the "Statues" in the position they were when the game was saved.

The variables of the "SaveInfo" Blueprint are:

All variables were marked as "Editable" (represented by the icon with the open eye) to facilitate access to these variables in other Blueprints.

The Blueprint "SaveInfo" is used only to store the information. Nothing is created in its EventGraph.

In this example the Blueprint "GameHud" will be responsible for the option of Quick Save and Quick Load. We will add a variable of type SaveInfo to "GameHud" to fill with the information that will be saved. The name of the variable is "SaveInfoVar".

In Blueprint "GameHud" we will add the F9 key Event for the Quick Save and the F10 key Event for the Quick Load. In order for the Input Events to work in Blueprint "GameHud", you must use the Enable Input function as the image shows:

To organize the Blueprint I created the Macros SaveGameVars, SaveTransforms, LoadGameVars and LoadTransforms. They are used to move the information between the Blueprint SaveInfo and the Blueprints of the game. The contents of these Macros will be shown later.

This is the implementation of Quick Save:

Click to enlarge

Let's look at each of the Functions and Macros used:
  • Create Save Game Object: Creates an object of the class selected in the parameter "Save Game Class". In our example the class used is "SaveInfo". 
  • Cast To SaveInfo: Converts to the SaveInfo class the object that was created.
  • Set SaveInfoVar: Stores the object created in the SaveInfoVar variable.
  • Macro SaveGameVars: Copies the values of the variables Level, Score, StatueCont and Time from the GameHud to the SaveInfoVar object.
  • Macro SaveTransforms: Copies the values of the Transformations of the Player and the Statues to the SaveInfoVar object. 
  • Save Game to Slot: Saves the SaveInfoVar object, using as SlotName = "BP_Game" and UserIndex = 0.

When the Quick Save is performed, the Blueprint will create a file using the Slot Name and the extension ".SAV". In this example the BP_Game.SAV file was created in the "ProjectName->Saved->SaveGames" folder. This file contains the data of all the variables we created in the Blueprint SaveInfoThe UserIndex parameter allows multiple SaveGame objects being saved on the same file. To do this, change the value of UserIndex.

Below is the implementation of Quick Load

Click to enlarge

The Functions and Macros used:
  • Does Save Game Exist: Checks for a saved game with the informed SlotName and UserIndex.
  • Branch: Used so that the next functions are only executed if there is a saved game.
  • Load Game from Slot: Load a game from a file and creates an object containing the information that was saved.
  • Cast To SaveInfoConverts to the SaveInfo class the object that was created.
  • Set SaveInfoVarStores the object created in the SaveInfoVar variable.
  • Macro LoadGameVars: Copies the values of the variables Level, Score, StatueCont and Time from the SaveInfoVar object to the GameHud.
  • Macro LoadTransforms: Copies the values of the Transformations from the SaveInfoVar object to the Player and the Statues.

To conclude the article we will examine the contents of each of the created Macros.

  • Macro SaveGameVars:

Click to enlarge

In this Macro we are just storing the values of GameHud variables in the variables of the SaveInfoVar object.

  • Macro SaveTransforms:

Click to enlarge

The player Transformation is obtained using the GetPlayerCharacter and GetActorTransform functions. To save the Transformations of the "Statues" we use Arrays with ForEachLoop

We use the "Get All Actor of Class" function to get an array containing all the "Statues" that are in the game. Using the ForEachLoop we get the Transformation of each "Statue" and add it to the StatuesTransforms Array of the SaveGameInfo object.

  • Macro LoadGameVars:

Click to enlarge

This Macro was based on Macro StartGame. But instead of using default values to start the variables, it is being used the loaded values of the SaveInfoVar variable.

  • Macro LoadTransforms:

Click to enlarge

Performs the reverse process of the SaveTransforms Macro, copying the values of Transformations from the SaveInfoVar object to the Player and the Statues.