
Scripting
Namespace
All code for this asset is under the following namespace(s):
// General Code
CarterGames.Assets.SaveManager
To access code from the asset you will need to be using the asset namespace in your script or use the full path of the asset script in order to access it.
Scripting
Classes
All methods in class are static and can be accessed by using class name before the method without needing a reference to an instance of the script.
// Example of usage... replacing "MyMethodHere" with the method you want to call.
SaveManager.MyMethodHere
Save Manager
Methods
SaveGame()
Saves the data provided into the method into the save file.
Example Usage
SaveManager.SaveGame(SaveData data);
LoadGame()
Loads the game data from the file or returns a warning if not data was found.
ReturnsSaveData
– SaveData containing and save data that was in the save file.
Example Usage
var data = SaveManager.LoadGame();
HasSaveFile()
Checks to see if a save file exists and returns it for use.
Example Usage
if (SaveManager.HasSaveFile())
ResetSaveFile()
Resets the save file to the default values.
Example Usage
SaveManager.ResetSaveFile();
DeleteSaveFile()
Deletes the current save file.
Example Usage
SaveManager.DeleteSaveFile();
Scripting
Example
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 example scene of the asset.
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);