How to handle altered gamedata?

Discussion and help for Easy Save 3
Post Reply
rubine2323
Posts: 6
Joined: Wed Jan 19, 2022 11:52 pm

How to handle altered gamedata?

Post by rubine2323 »

I would like to know whats the best practise to handle altered gamedata?

For example gamedata was saved like that:

Code: Select all

Gamedata{
 int version
 LevelData leveldata
}

Leveldata{
 int value1
}

now i change the Leveldata to

Code: Select all

Leveldata{
 int value1
 int value2
}
On loading with an old savegame without int value2 in it it will throw an error.

What would be the best practise in my case to migrate to the new structure.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to handle altered gamedata?

Post by Joel »

Hi there,

The example you've given shouldn't throw an error (not from our end anyway). In the case of loading your data it will load value1 as expected and do nothing with value2.

In this case you would need to set an appropriate default value for your value2 field.

If you want to perform logic based on version number then you would simply check your version number field and perform logic accordingly. There's no way of Easy Save to automatically know what logic you would want to perform.

Just a note, generally you would put your version number in a seperate key in your file, rather than putting it in the script itself. This means you can access the version number before loading the data. You also don't need different version numbers for each iteration of your script, just a version number for each build. E.g.

Code: Select all

// To save
ES3.Save("version", Application.version);

// To load
var version = ES3.Load<string>("version", null);

if(version == null)
{
	// This would be your last version before adding version numbers. So we can load normally.
	gameData = ES3.Load<Gamedata>("gameData");
}
else if(version == "1.1")
{
	// This would be your last version before adding version numbers. So we can load normally.
	gameData = ES3.Load<Gamedata>("gameData");
}
else if(version == "1.2")
{
	// etc
}

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
rubine2323
Posts: 6
Joined: Wed Jan 19, 2022 11:52 pm

Re: How to handle altered gamedata?

Post by rubine2323 »

This will actually throw an error if its structured like that:

Code: Select all


public class GameData
{
    public int saveFileVersion;
    public GlobalData globalData;
    public List<BaseLevelData> levels;
    public List<LevelSelectionData> levelSelectionData;
}

public class BaseLevelData
{
    //GENERAL
    public string sceneName;

    public ObscuredInt exp;
    public ObscuredInt maxExp;
    public ObscuredInt level;

    public ObscuredInt prestigeLevel;

    public ObscuredFloat power;
    public ObscuredFloat maxPower;
    public List<float> powerLevels = new();

    public string secretRoomPin; <--------------------------------- New added variable
    public DateTime timeCodeEntered; <--------------------------------- New added variable
    public int attempts; <--------------------------------- New added variable
}
Now if i add these new variables it attempts to load the BaseLevelData like it was structured before

Code: Select all

    public void LoadLevel(string levelName)
    {
        if (ES3.KeyExists(levelName, saveFileName))
        {
            if (levelName == "Level1Scene")
            {
                BasicLevel loadedLevelData = ES3.Load<BaseLevelData>(levelName, saveFileName) as BasicLevel;

                // Find and replace the level data in your list
                int index = currentGameData.levels.FindIndex(level => level.sceneName == levelName);
                if (index != -1)
                {
                    currentGameData.levels[index] = loadedLevelData;
                }
                else
                {
                    // If the level doesn't exist in the list, add it
                    currentGameData.levels.Add(loadedLevelData);
                }
            }
        }
        else
        {
            Debug.LogWarning("No saved data found for level: " + levelName);
        }
    }
rubine2323
Posts: 6
Joined: Wed Jan 19, 2022 11:52 pm

Re: How to handle altered gamedata?

Post by rubine2323 »

Okay weird i reverted my savefile to my previous before implementing the changes to the structure so its in the state like it was before.

Then i loaded the old save data it worked.

I added the changes again and it loaded them like nothing happend. Maybe i glitched something but youre right it just works. Need to further investigate what caused the issue before.

Thank you for your time.
Post Reply