Game State Saves - UQcsse3200/2024-studio-2 GitHub Wiki
Introduction
The game will make use of saving and loading for various features, including settings, stats, progression and inventory. This data will be stored in data structs in GameState, and saving and loading will be handled by SaveHandler.
Key Components
- GameState - Contains all non run-shared save file data structs as members.
- Achievements - Contains all run-shared save file data structs as members.
- SaveHandler - Handles saving and loading of GameState as JSON files.
- QuestSave - Serializes quest data to and from JSON. See here for more information.
- InventorySave - Serializes inventory data to and from JSON. here for more information.
- PlayerSave - Serializes player data to and from JSON.
Usage
Add your data struct as a static field in GameState/Achievements and instantiate it:
public class GameState {
public static EnvironmentVariables env = new EnvironmentVariables();
}
To access the SaveHandler, you must call getInstance as it is a Singleton implementation:
SaveHandler.getInstance();
To save all fields in GameState:
SaveHandler.getInstance().save(GameState.class, "saves", FileLoader.Location.LOCAL);
Save files are currently saved to "/assets/", consistent with the libgdx local file default. Check the javadocs of GameState to see exactly where you should save your data files to.
To load all fields in GameState:
SaveHandler.getInstance().load(GameState.class, "saves", FileLoader.Location.LOCAL);
If you are loading from the default saves, use Location.INTERNAL instead.
To clear saves:
SaveHandler.getInstance().delete(GameState.class, "saves", FileLoader.Location.LOCAL);
To clean the fields of GameState or Achievement but not remove the JSON saves:
GameState.clearState();
If data that you add is non-primitive (eg. ArrayList), you may need to manually serialize the class by implementing JSON.Serialize. Please see here for more information.
Unit Testing
- SaveHandlerTest covers read, write and deletion and ensures all of these methods work as intended.
- Unit testing for the QuestSave struct serialization as well as additional save/load testing is performed in QuestManagerTest.
- Unit testing for the InventorySave struct serialization as well as additional save/load testing is performed in InventoryTest.
QA Testing
1. Functionality Testing:
To test the functionality of saves, we have a step by step process described below:
- Add relevant information to save data through gameplay.
- Close and reopen the game, as well as just returning to main menu and reloading.
- Ensure that the game data properly loads when clicking load, and is reset when clicking start.
2. Usability Testing:
- Check that the quest overlay accurately reflects the tasks required for completion.
- For development, ensure that serialization remains clear for all save data.
3. Performance Testing:
- Ensure that saving and loading JSON files with large amounts of data does not lag the game.
UML Diagram
Note: the green unlock symbol means public, the orange key means protected and the red lock means private. the little hats on the (f) means final variable and, the star on the (f) means static variable