Storage Module Usage ==================== The storage module contains several cmoponents that can be used separately or in combination for the following tasks: - Settings store that holds game settings persistently: (machine-specific, user-specific and achievements) - A save slot manager through which a game can manage multiple player profiles in a very simple fashion - Game state store that holds informations about the game's state and allows this to be saved - Some serialization helpers including a tamper-detecting XML serializer Usage: Game Saving ------------------ Step 2: State Class - Write class that holds the game state you wish to save This class will store everything the game needs to know across scene changes. It can also be saved into a file and loaded again at a later time to provide game saving functionality. It is possible (and encourages) to create sub-classes to structure the game state. The game state root class that we will write here needs to implement the IGameState interface (which is inteded to other modules to access common settings) /// Stores the current state of things in the doodle game public class DoodleGameState : IGameState { /// How much health the player has left public int PlayerHealth = 3; /// Date and time the player last played the game public DateTime LastPlayed; /// How long the player has played with this profile public TimeSpan PlayingTime; /// Last time the user played on this state (UTC time) DateTime IGameState.LastPlayed { get { return this.LastPlayed; } set { this.LastPlayed = value; } } /// For how long the user been playing this state in total TimeSpan IGameState.PlayingTime { get { return this.PlayingTime; } set { this.PlayingTime = value; } } /// How far along the game the state is float IGameState.Progress { get { return 0.0f; } } } Step 2: Initialization - Write a game-specific adapter component The settings and savegame stores need to be initialized exactly once during your game's lifetime. Of course, we do not want to lose the ability to just open any scene and play it in the editor, so to handle initialization, we will write an adapter component that is specific to each game. /// Provides, saves and loads the current game state public class DoodleGameStateStore : ScriptComponent { /// Current game state that will persistent across sessions public DoodleGameState State { get { return this.gameStateStore.GameState.Access(); } } /// Injects the instance's dependencies /// Store holding the current game state /// /// This method is called automatically by the services framework. Any parameters /// it requires will be filled out by looking up the respective services and creating /// them as needed. /// protected void Inject(IPersistentGameStateStore gameStateStore) { this.gameStateStore = gameStateStore; if(!this.gameStateStore.IsInitialized) { this.gameStateStore.Initialize(); } } /// Holds the current game state and allows saving it private IPersistentGameStateStore gameStateStore; } Step 3: Call Load() / Save() as needed The persistent game state manager you obtain from the Inject() method above (via the dependency injection service) offers methods like StartNewGame(), LoadGame(), SaveGame() and DeleteGame(). Call these from your game's menu or in any other situation where the player wants to begin a new game, load or save the game's state. Step 4: Write a custom serializer (optional) If you want to change the file format (for example, going binary instead of XML) or tamper-protect your game's saves, you need to implement a custom serializer. This can be very little code because you can just forward to the standard serializer and add the things you want (i.e. delegates for reading and writing the checksum from the state for the TamperDetectingSerializer instead). Through custom serializers, it's also possible to change the file name of the game state file, or to write additional files (i.e. index data so your game can quickly load summary information about a saved game and display this to the player). Usage: Settings --------------- Same as game saving, but use IPersistentSettingsStore