// Method for loading and serializing all game data saves stored into disk. private int loadDiskDataSaves() { FileInfo[] savedGameDataFiles = GetFilesByExtensions(new DirectoryInfo(SavingLocation), ".json", ".bson", ".xml", ".bin"); // No data saves found on disk if (savedGameDataFiles.Length == 0) { return(0); } // Sort by creation-time descending Array.Sort(savedGameDataFiles, delegate(FileInfo f1, FileInfo f2) { return(f2.CreationTime.CompareTo(f1.CreationTime)); }); for (int i = 0; i < savedGameDataFiles.Length; i++) { SerializedStoredData serializedStoredDataAux = LoadFromFile <SerializedStoredData>(savedGameDataFiles[i].Name); if (!object.Equals(serializedStoredDataAux, default(SerializedStoredData))) { diskDataSaves.Add(serializedStoredDataAux); } } return(savedGameDataFiles.Length); }
// This method gets current game state from scriptable objects // inside the project and assigns the references to bufferedGameState private SerializedStoredData getGameState(string filename = null) { SerializedStoredData gameState = new SerializedStoredData(); // Scene gameState.gameData.sceneName = sceneController.startingSceneName; gameState.gameData.startingPositionName = sceneController.initialStartingPositionName; // Conditions if (AllConditions.Instance != null) { gameState.gameData.conditions = AllConditions.Instance.conditions; } else { Debug.Log("No AllConditions Asset Found inside project resources"); } // Inventory gameState.gameData.itemsNames = inventory.getCurrentItemsNames(); // Save datas gameState.gameData.gameSaveDatas = allProjectSaveDatas.ToArray(); if (filename != null) { gameState.gameData.fileName = filename; } return(gameState); }
public void newGame(string fileName = null) { // Initializing bufferedGameData with all scriptable objects and inventory empty bufferedGameData = getGameState(); // Set file name bufferedGameData.gameData.fileName = (fileName != null) ? fileName : getValidFileName(); }
public void saveGame() { // Buffer current gamestate with the existing filename bufferedGameData = getGameState(bufferedGameData.gameData.fileName); // Set the save date bufferedGameData.gameData.saveDate = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"); // Save to file SaveToFile(bufferedGameData, bufferedGameData.gameData.fileName); Debug.Log("File: " + bufferedGameData.gameData.fileName + " saved in directory: " + SavingLocation); }
public void loadGame(int index) { // Set buffered game data to the disk save data state bufferedGameData = diskDataSaves[index]; // Push all the data from the buffered game data // to all the scriptable objects of the project setGameState(bufferedGameData); // Load the next scene once all the data has been loaded sceneController.FadeAndLoadScene(bufferedGameData.gameData.sceneName, bufferedGameData.gameData.startingPositionName); // clear all the disk data saves from this object (memory performance). clearDiskDataSaves(); }
// This method gets the values of data parameter // and assign them to scriptable objects inside the project private void setGameState(SerializedStoredData data) { // Allconditions if (AllConditions.Instance != null) { // Set the conditions to the saved game state AllConditions.setConditionsState(data.gameData.conditions); } else { Debug.Log("No AllConditions Asset Found inside project resources"); } // Inventory if (inventory) { // Set the inventory inventory.setInventoryState(data.gameData.itemsNames); } else { Debug.Log("No Inventory found"); } // Data Saves for (int i = 0; i < data.gameData.gameSaveDatas.Length; i++) { int saveDataIndex = allProjectSaveDatas.IndexOf(allProjectSaveDatas.Find(saveData => saveData.name == data.gameData.gameSaveDatas[i].name)); if (saveDataIndex != -1) { allProjectSaveDatas[saveDataIndex] = data.gameData.gameSaveDatas[i]; } else { Debug.LogError("Data Save: " + data.gameData.gameSaveDatas[i].name + " found on save data file but not present in all Game Save Datas"); } } }