Save Games
Overview
NeoFPS uses a comprehensive but flexible system for saving game state which is designed to achieve the same effect you see in top single-player first person shooters. The player should be able to quick-save the game at any point without a perceptible freeze, and then re-load to the exact same state down to the level of bullets in flight and rigidbodies mid-fall.
The system can also be set up to save as much or as little information as desired. The mentioned quick-saves might be the desired effect for a narrative game, but a rogue-lite style FPS might want to save limited information on inventory and character skills only, while a first person platformer might want to use checkpoints that save the active spawn point and score.
Quick, Auto and Manual Saves
The save system has 2 user controlled save types that are enabled by default. On standalone builds, the player can quick-save using the F5 key, and quick-load using the F9 key (these can be remapped in the game options). The player can also manually save the game from the in-game menu and give the save a specific name to help understand what point in the game they had reached (commonly called a hard save).
In most modern FPS games it is also common to save the game state at specific points in a scene or after specific actions. The save system can trigger auto-saves via scripts, and includes an example behaviour called AutoSaveBehaviour.
Each of the above save types can be enabled or disabled in the SaveGameManager settings.
Save Browser
The NeoFPS samples provides a save game browser in the main menu, implemented in the NeoFPS sample UI system. The same functionality can be recreated fairly simply in another UI, though it requires some scripting knowledge.
The save browser lists all the of the available saves, along with the following details:
- Name - For manual saves, this can be set by the player. For quick and auto saves, this is set in the scene save info.
- Save Type - Quick, auto or manual save.
- Save Date & Time - The moment the save button was hit.
- Thumbnail - An image that represents the save. For quick and auto saves, the save system can take a screenshot on save, get a texture from the scene save info, or get a texture from the save game manager. Manual saves cannot use a screenshot (it would just be a menu).
There is also a version of the save browser in the in-game menu which only listss manual saves, and allows you to create a new manual save or choose one of the existing ones to overwrite.
From the main menu, you can also use the Continue button to load from the latest save (this can be set to specific types or all types in the manager).
Overrides and Persistence
Besides the standard game saves, the save game system can also be used for other purposes where serializing and deserializing scene or object date is required (either to disk or to a memory buffer). One example of this is persisting character data between scenes as the player progresses through the game. In this situation, the game mode tells the save manager to save the data for the player and character objects, and then reloads them when the new scene has loaded.
In the case of persistence, it is only desired to save and load specific data such as the character health and inventory. Information such as character position, aim and animation states could actually cause some major problems if they did not match the new scene. To achieve this, the NeoFPS save system supports a feature called overrides. You can specify different save modes, and then each object can override what information is saved for each mode.
For more information see Overrides and Persistence.
Controlling What Data Is Saved
The NeoFPS save system requires some setup for objects to control what information is saved and loaded. You can specify individual child objects and components to either include in or exclude from the save.
Components can come in 2 forms. Neo-serialized components are MonoBehaviours which implement the INeoSerializableComponent interface. This is the best method of serialization. If you want to serialize a component that you do not have control over the script for (such as an inbuilt Unity component, or a script from another asset), there must be a formatter created for it which understands what data to save and load.
For more information see Serializing Data.
Objects that are instantiated at runtime can also be recreated from their prefabs as long as that prefab is registered with either the save game manager or the scene save info for the scene it is instantiated in. Unity does not provide information on whether an object is being created as part of a scene load, or a runtime instantiation. This means that in order for the objects to be correctly registered in the scene hierarchy, there are some rules about how to instantiate prefabs and where.
For more information see Runtime Objects.
Troubleshooting
In the event that data is not being saved properly there are a few common mistakes to look out for, and the save game inspector can be used to examine saved data and check where the problem lies.
For more information see Troubleshooting Saves.