/// <summary> /// Loads the stored game state to session. /// </summary> public void Load() { // Designed to be only called once without recalls to file data. // Incorrect calls could overwrite data. string jsonPath = Application.persistentDataPath + "/gamesave.save"; if (File.Exists(jsonPath)) { // 1. Fetch from path GameSaveState saveState = JsonUtility.FromJson <GameSaveState>(File.ReadAllText(jsonPath)); //Debug.Log("State has been loaded"); // 2. Read saved instances HangarInventory hangarCurrentSave = saveState.GetHangarSave(); shipServicer.SetHangarShips(hangarCurrentSave.hangarShips); weaponServicer.SetHangarShips(hangarCurrentSave.hangarWeapons); userStatus = saveState.GetUserStatus(); if (hangarCurrentSave == null || saveState.userStatus == null) { Debug.LogWarning("Detecting missing or data loss"); SetupDefaultPlayer(); } } else { SetupDefaultPlayer(); } }
/// <summary> /// Resets current save state and loads defaults. /// </summary> public void ResetAllSaves() { GameSaveState newGameSave = new GameSaveState(); string jsonData = JsonUtility.ToJson(newGameSave, false); string jsonPath = Application.persistentDataPath + "/gamesave.save"; File.WriteAllText(jsonPath, jsonData); Debug.Log("Finihsed Reset"); SetupDefaultPlayer(); }
/// <summary> /// Saves current state of game into device storage. /// </summary> public void Save() { // This can be called multiple times throughout the session. // 1. Create new game save GameSaveState newGameSave = new GameSaveState(); HangarInventory hangarInventory = new HangarInventory(); hangarInventory.hangarShips = shipServicer.GetHangarShips(); hangarInventory.hangarWeapons = weaponServicer.GetHangarWeapons(); // 2. Write saved instances newGameSave.SaveHangar(hangarInventory); newGameSave.SaveUserStatus(userStatus); // 3. Serialise file and Save string jsonData = JsonUtility.ToJson(newGameSave, false); string jsonPath = Application.persistentDataPath + "/gamesave.save"; File.WriteAllText(jsonPath, jsonData); Debug.Log("Finihsed Writing"); }