Forum rules - please read before posting.

Any way to continue from last save file without Options Data?

Hello everyone. I'm using AC v 1.74.2

I have a "continue“ button in my main menu. I'm using the "continue from last save" node. It works great when I'm on a single computer. However, I noticed that it uses OptionsData to determine the last save from the ID.

    public static bool ContinueGame ()
    {
        if (Options.optionsData != null && Options.optionsData.lastSaveID >= 0)
        {
            return LoadGame (Options.optionsData.lastSaveID);
        }
        return false;
    }

This causes problems when trying to continue on another device with synced save files. The options data is saved to player prefs, and thus, not carried over to the new device. So on the new device, the lastSaveID might be different or not exist at all. So is there some setting or something I could do to safely retrieve the last save? Or a way to edit the script to do so?

Comments

  • You can switch your Options file handled to OptionsFileHandler_SystemFile to have Options data be placed in files in the same way as save-games:

    SaveSystem.OptionsFileFormatHandler = new OptionsFileHandler_SystemFile ();
    

    Otherwise, a custom script could read the save files themselves and work out which has the latest modified timestamp.

    A function along these lines should return the ID of the latest save this way:

    int GetLatestSaveID()
    {
        string saveDirectory = SaveSystem.PersistentDataPath;
        string filePrefix = KickStarter.settingsManager.SavePrefix;
    
        int largestWriteTime = 0;
        int largestWriteTimeID = 0;
    
        DirectoryInfo dir = new DirectoryInfo(saveDirectory);
        for (int i = 0; i < KickStarter.settingsManager.maxSaves; i++)
        {
            int saveID = 0;
    
            string filename = filePrefix + SaveSystem.GenerateSaveSuffix(saveID);
            string filenameWithExtention = filename + SaveSystem.GetSaveExtension();
    
            var info = dir.GetFiles(filenameWithExtention);
            if (info == null) continue;
    
            foreach (FileInfo fileInfo in info)
            {
                if (fileInfo.Name != filenameWithExtention) continue;
    
                var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                TimeSpan diff = fileInfo.LastWriteTime.ToUniversalTime() - origin;
                var writeTime = (int)diff.TotalSeconds;
    
                if (writeTime > largestWriteTime)
                {
                    largestWriteTime = writeTime;
                    largestWriteTimeID = saveID;
                }
            }
        }
    
        return largestWriteTimeID;
    }
    
  • Oh great! So I could just add this to a script in my main menu and set Options.optionsData.lastSaveId to this?

  • Either that, or store it in a Global Variable that's used to load the save by ID directly.

    Before that, though, check that the returned value is correct.

  • I tried that in the editor and it worked fine after some minor adjustments. However, it didn't work on Xbox because the files are different. I was thinking of using the updateTime stored on the SaveFile.

    How could I write it to get each save's time from Kickstarter.saveSystem.foundSaveFiles?

    I tried using that but that value is a very big negative number and it seems that if I just compare them using >, the oldest one is the bigger number.

  • If you're porting to Xbox, what is your SaveFileHandler? It's in that script that updateTime is set.

    I tried using that but that value is a very big negative number and it seems that if I just compare them using >, the oldest one is the bigger number.

    If both are negative, is that to say the oldest one has the largest absolute value, or the one closest to zero.

    v1.74.2 is very old now, and it's possible there is an issue with the way timestamps were recorded, but the values should still be ordered and a comparison of updateTime should be OK To do.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.