Issue with saving system

Hello,

I’m currently working on a game project on which I wanted to implement a save system allowing the player to create multiple worlds. I did the saving and loading mechanics, which work well, but now that I’m trying to expand it for the multiple worlds idea, it started doing weird things…

In order to know which saved folder is corresponding to which world, I’ve instantiated them with a number. This number is set through PlayerPrefs. I’ve tested my code, and the PlayerPrefs part works… but the folders either don’t create themselves, or they don’t have the same number as the PlayerPrefs. This causes issues both in the creating/saving of a first world, and the loading because it isn’t taking the right number into consideration when checking the folders.

This is the code that I have as of now in my multi-saving script:

// Getting all the folders in my SAVE_FOLDER path
private void GetAllSaveFiles()
    {
        files = System.IO.Directory.GetDirectories(SaveFunctions.SavingPath());

        totalSaves = files.Length;
    }

// The function to load the scene
    public void SelectSavedFiles(int indexSave)
    {
        PlayerPrefs.SetInt("indexSave", indexSave);
    }

// The function to create a new save
    public void StartNewGame()
    {
        saveIndex = totalSaves + 1;
        PlayerPrefs.SetInt("indexSave", saveIndex);

        SaveFunctions.CreateFolder();
    }

And this is in my saving functions one:

public static class SaveFunctions
{
    private static readonly string SAVE_FOLDER = Application.persistentDataPath + "/Saves/";
    private static readonly string SAVE_FOLDERS = Application.persistentDataPath + "/Saves/world_" + PlayerPrefs.GetInt("indexSave") + "/";
    public static void Init()
    {
        if (!Directory.Exists(SAVE_FOLDER))
        {
            Directory.CreateDirectory(SAVE_FOLDER);
        }
    }

    public static string SavingPath()
    {
        if (Directory.Exists(SAVE_FOLDER))
        {
            return SAVE_FOLDER;
        }
        else
        {
            return null;
        }
    }

    public static void CreateFolder()
    {
        if (!Directory.Exists(SAVE_FOLDERS))
        {
            Directory.CreateDirectory(SAVE_FOLDERS);
        }
    }
}

It’s the first time I’m really working on a save system like that, so I might have made a really stupid mistake, but I’ve been spending ages reading and testing this code again and again without finding the issue… When I debug the PlayerPrefs, everything’s good, so it confuses me a lot

Thank you so much in advance for your help

Edit: I think I’ve figured that the issue is that my SAVE_FOLDERS only takes the PlayerPrefs at the full beginning. I’m not exactly sure how to make this change in run-time though…

Edit: I managed to figure a solution by myself in the end. For those wondering, I deleted the SAVE_FOLDERS string and instead directly used SAVE_FOLDER + folderName + PlayerPrefs in each place where I needed it

Good to see that you found a solution to your problem, though I wonder if recording the number of saves is necessary?

I suppose it depends on whether these are ‘worlds’ in the typical sense, such as Minecraft or Valheim, or if these worlds are, iunno, smaller components of a large save file?

In the former case, I would just let the player name the world, make the save file/folder named after the desired name, and when comes time to loading them you can just Directory.GetFiles to retrieve the path to all the save files and use that to let the player select the world they want to load (like through a profile/world select screen).

For the later you could use a master save file to keep track of any sub-worlds.

It’s personal preference, but I would avoid using player prefs altogether.