The `SaveGame` class sets up an object that can be used as a target for the saving and loading functions declared in `Kismet/GameplayStatics.h`.
You can create a new class based on `SaveGame` using the [](Programming\Development\ManagingGameCode\CppClassWizard).
(w:800)
In this example, the new `SaveGame` class is called `MySaveGame`.
### Header
In the header file for your `SaveGame` object, you can declare any variables you want your `SaveGame` to store.
UPROPERTY(VisibleAnywhere, Category = Basic)
FString PlayerName;
[REGION:note]
In this example, there are also variables declared that will be used to store default values for the `SaveSlotName` and the `UserIndex`, so that each class that saves to this
`SaveGame` object will not have to independently set those variables. This step is optional, and will cause there to be one save slot that gets overwritten if the default values are not changed.
Generally, the `SaveGame` object's source file does not need any particular code to function, unless your particular save system has additional functionality you would like to set up
here.
This example does define the values of `SaveSlotName` and `UserIndex` in the class constructor, so they can be read out and used by other gameplay classes.
To be able to easily access both your `SaveGame` object and the creating, saving, and loading functions from `GameplayStatics`, you should add the following lines to your game module's header file,
Whenever you want to save out a variable to your `SaveGame` object, you must create an instance of your `SaveGame` object, and then set the variable within it.
To load a variable, you must first load the `SaveGame` object using `UGameplayStatics::LoadGameFromSlot`. This creates a new instance of the `SaveGame` object.
Again, in this case, an empty `SaveGame` object is first created, so the default `SaveSlotName` and `UserIndex` can be read from it. This is an optional step and may not apply in all game implementations.
Once the new `SaveGame` object has been loaded from the hard drive, the variable values can be read from it and assigned to the necessary Actors or classes, or used directly as shown here.