Scripting API

Namespace

All code for this asset is under the following namespace, when using the manager you will need to add this namespace to your using defines.

// Asset namespace
CarterGames.Assets.SaveManager

// Using line for the asset
using CarterGames.Assets.SaveManager

Save Manager Methods

Below are all the public methods and properties that are provided in the save manager asset that you use. Note that some are excluded as they are only for the editor side of the asset to work.

// Method - Saves the data provided into the method into the save file....
SaveGame(SaveData data); 

// Method - Loads the game data from the file or returns a warning if not data was found...
// Returns: SaveData containing and save data that was in the save file...
LoadGame();

// Method - Checks to see if a save file exists and returns it for use...
// Returns:Bool
HasSaveFile();

// Method - Resets the save file to the default values... 
ResetSaveFile();

// Method - Deletes the current save file...
DeleteSaveFile();

Saving & Loading My Game

Saving and loading your game is somewhat subjective as each game requires a different setup. However, most games will want to load their data on the start of the game, edit it when levels are complete and save it when progress is made. We recommend having a static or persistent object that holds a copy of the save data class as a variable which can be edited on the fly and using in the saving and loading process. Below are some examples of how to go about each stage. These can also be seen in the example script in the asset which is used in the

Loading

Here we’re assuming that you have an empty GameObject or similar which holds this script to load the game when the scene starts. This is one of the better ways of loading the game if you need the data in each scene.

// Gets the game save from the file....
SaveData loadData = SaveManager.LoadGame();

// Applies the values to the 
player.name = loadData.playerName;
player.health = loadData.playerHealth.ToString();
player.trandform.position = loadData.playerPosition.ToString();
player.sprite = loadData.playerSprite;

Saving

Saving can be done whenever, however it is recommended that you only save when necessary to avoid an impact to your games performance.

// Makes a new save data class instance for use...
SaveData saveData = new SaveData();

// Applies the values to the class for saving...
saveData.playerName = player.name;
saveData.playerHealth = player.health;
saveData.playerPosition = player.transform.position;
saveData.playerSprite = player.sprite;

// Saves the game with the class creates/edited above...
SaveManager.SaveGame(saveData);