Developing my first game, and trying to get the save/load system working. I followed this tutorial, https://www.youtube.com/watch?v=aUi9aijvpgs, which got me started, but I’m running into some issues in the actual practice.
Initially, when following that tutorial, I had my game data (current level, progress stats, etc.) and player preferences (difficulty settings, audio/visual settings, etc.) being managed in one file data system, but I was having trouble getting the player preferences to carry over between sessions; ie, when you mute the music at the start of the game, it remains muted through that session, but when you exit the application and reopen, it returns to the default music volume setting instead of remembering that you wanted it muted.
At the moment, I only have the first level of the game completed, and for this game I’m only having it do an autosave at the end of each level as opposed to having a manual save system, so I wasn’t able to test whether the game data was saving or not, but I added in a “test” end of level, and confirmed that the previously completed game data is not loading in a new session either.
In my attempts to trouble shoot, I ended up splitting the game data and preferences into two separate file data systems by duplicating what I’d setup with the tutorial and splitting the data management accordingly, but unfortunately it’s still not loading the previous session’s settings when I start a new session, and I’m at a loss of what to try next.
In reviewing the debug log, it indicates that the preferences are being saved, but when I start a new session it indicates that there is no saved data to load. Part of my problem is that I’m not sure where the data files are being saved on the computer - in the Inspector, I’ve set the file names to save as “data.game” and “data.prefs” respectively (following the example from the tutorial), but I can’t find any files with either those in the program folders, so I guess it’s not actually generating those files…but I’m not sure what I did wrong, since I followed the tutorial as closely as I could.
I’m going to copy the sections of script that I believe are relevant here, but also attaching the full scripts in case there’s anything important I neglected to copy. Just going to show the scripts related to the Preferences Data, but the Game Data scripts are more or less the same:
- This is the Awake function from the Data Persistence Manager
private void Awake()
{
if (instance != null)
{
Debug.LogError("Data Persistence Manager: Found more than one Data Persistence Manager in the scene.");
Destroy(this.gameObject);
return;
}
instance = this;
DontDestroyOnLoad(this.gameObject);
if (disableDataPersistence)
{
Debug.LogWarning("Data Persistence Manager: Data Persistence is currently disabled!");
}
this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName, useEncryption);
this.prefsDataHandler = new PrefsFileDataHandler(Application.persistentDataPath, prefsFileName, useEncryption);
InitializeSelectedProfileId();
}
- This is the InitializeSelectedProfileId function from the Data Persistence Manager:
private void InitializeSelectedProfileId()
{
this.selectedProfileId = dataHandler.GetMostRecentlyUpdatedProfileId();
this.prefsSelectedProfileId = prefsDataHandler.GetMostRecentlyUpdatedProfileId();
if (overrideSelectedProfileId)
{
this.selectedProfileId = testSelectedProfileId;
this.prefsSelectedProfileId = prefsTestSelectedProfileId;
Debug.LogWarning("Data Persistence Manager: Overrode selected profile id with test id: " + testSelectedProfileId + " and " + prefsSelectedProfileId);
}
}
- This is the LoadPrefs function from the Data Persistence Manager
public void LoadPrefs()
{
// return right away if data persistence is disabled
if (disableDataPersistence)
{
return;
}
//Load Any Saved data from a file using the data handler
this.prefsData = prefsDataHandler.Load(prefsSelectedProfileId);
// if no data can be loaded, don't continue
if (this.prefsData == null)
{
Debug.Log("Data Persistence Manager: No prefs data was found. A New Game needs to be started before data can be loaded.");
return;
}
foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects)
{
dataPersistenceObj.LoadPrefs(prefsData);
}
Debug.Log("Data Persistence Manager: Loaded Show Collectibles on Map = " + prefsData.showCollectiblesOnMap);
//Not copying all of the Debug Logs here, for ease of reading, but I rinse and repeat this last line for each of the variables being stored in the Prefs Data
- This is the SavePrefs function from the Data Persistence Manager:
public void SavePrefs()
{
// return right away if data persistence is disabled
if (disableDataPersistence)
{
return;
}
// if we don't have any data to save, log a warning here
if (this.prefsData == null)
{
this.prefsData = new PrefsData();
Debug.Log("Data Persistence Manager: New Preferences Data File Created");
}
//pass the data to other scripts so they can update it
foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects)
{
dataPersistenceObj.SavePrefs(prefsData);
}
Debug.Log("Data Persistence Manager: Saved Show Collectibles on Map = " + prefsData.showCollectiblesOnMap);
//^^Rinse and repeat^^
}
Any suggestions on what I’ve done wrong here would be greatly appreciated.
DataPersistenceManager.cs (25.5 KB)
IDataPersistence.cs (303 Bytes)
PrefsFileDataHandler.cs (9.9 KB)