Forum rules - please read before posting.

Make saving location persistent in WebGL

Hi everyone,
On some portals the URL changes when you upload a new build and that results in having savings wiped out. I'm trying to find a way to make saving location persistent even after updating the game.
Found this article that suggests to replace “Application.persistentDataPath” with “idbfs/YOUR_GAME_RANDOM_CODE” but - leaving out the fact that I don't really know how to do it - AC Manual says that WebGL saves in PlayerPrefs and not in persistentDataPath.
I also had a look at iSaveFileHandler and SaveFileHandler_PlayerPrefs scripts as manual suggests but I'm afraid that the solution to override default saving location is far beyond my skills.

Anyone has any suggestion? Thanks a lot.

Comments

  • SaveFileHandler_SystemFile is the one that relies on Application.persistentDataPath. If you want to save a file outside of PlayerPrefs, that'll be the one to work with.

    You can create a subclass of this file and override its GetSaveDirectory function to rely on its regular functionality but with a custom save-directory:

    using UnityEngine;
    using AC;
    
    public class SaveFileHandler_Custom : SaveFileHandler_SystemFile
    {
    
        protected override string GetSaveDirectory (string separateProjectName = "")
        {
            return "idbfs/YOUR_GAME_RANDOM_CODE";
        }
    
    }
    

    This can then be assigned as the default handler with:

    SaveSystem.SaveFileHandler = new SaveFileHandler_Custom ();
    
  • Thanks a lot Chris.
    I placed the first script with DontDestroyOnLoad in a game object in my very first scene.
    Where do you suggest I should put the following code?

    SaveSystem.SaveFileHandler = new SaveFileHandler_Custom ();

    Thank you in advance and sorry for the noob questions!

  • Soooo, strip what i wrote before.

    I created a script named SaveFileHandler_Custom.cs with the following code

    
    using UnityEngine;
    using System.IO;
    using AC;
    
    public class SaveFileHandler_Custom : SaveFileHandler_SystemFile
    {
        string savePathName;
    
        protected override string GetSaveDirectory(string separateProjectName = "")
        {
            savePathName = "idbfs/myGameName";
            if (!Directory.Exists(savePathName))
            {
                Directory.CreateDirectory(savePathName);
            }
            return savePathName;
        }
    
    }
    
    

    then in my very first scene I have an empty object with the following script

    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;
    
    public class OverrideSave : MonoBehaviour
    {
        private static bool created = false;
    
        // Start is called before the first frame update
        void Start()
        {
            SaveSystem.SaveFileHandler = new SaveFileHandler_Custom();
        }
        void Awake()
        {
            if (!created)
            {
                DontDestroyOnLoad(this.gameObject);
                created = true;
            }
        }
    
    }
    

    Now saving works fine in the original URL. If I change the URL it detects that a saving file exists (as I have a reset button that shows up only if save exists and it does show up), but when I press play it's not loading correctly (Play button should continue from last save but when pressed it ends up on a black screen).

    Where am I doing wrong?
    Thanks again.

  • By "change the URL", are you referring to the savePathName string, or where your built game is loaded from? If the former, are you testing this in the Editor?

    If this is from a build, locate Unity's log files to see if they reveal what problem it's coming across.

    Try also using the default Menu Manager to access the Load menu, which displays all-found saves along with their labels. Are saves correctly listed?

  • edited April 2022

    By "change the URL", are you referring to the savePathName string, or where your built game is loaded from? If the former, are you testing this in the Editor?

    I simply rename the folder on the server where the game is hosted, to simulate what happens when you upload a new build on some portals.

    If this is from a build, locate Unity's log files to see if they reveal what problem it's coming across.

    So, this test if from a build uploaded on my server and running in browser and nothing is shown in Unity log files.

    Try also using the default Menu Manager to access the Load menu, which displays all-found saves along with their labels. Are saves correctly listed?

    I had deleted/edited all existing menus but I've added a savelist item in my main menu as List Type load and it shows autosave both before and after having changed the URL. What's strange is that it (the savelist item) works correctly all the time while my Play button does not.
    It simply set a couple of variables and then checks if autosave exists, if so continue from last save, otherwise it switches to the intro scene.
    Pressing it after having changed the URL brings to a black screen (actually it does nothing, it seems to be a black screen just because I was fade camera out), while the savelist works as expected.
    What's the difference between what the SaveList item does and the "Continue From Last Save" Action?

  • I'm not familiar with itch.io saving - is the expected behaviour that changing the URL should still allow for previous saves? Or is the issue that the Autosave should not be present at all?

    nothing is shown in Unity log files.

    Try accessing your browser's developer console to see if that reveals anything.

    What's the difference between what the SaveList item does and the "Continue From Last Save" Action?

    Nothing in practice, but I'm asking to help debug the problem. The text displayed in the SavesList element may reveal more about what information it's able to distract.

  • I'm not familiar with itch.io saving - is the expected behaviour that changing the URL should still allow for previous saves? Or is the issue that the Autosave should not be present at all?

    I'm not either, the issue is on my publisher site that basically has the same behavior: Every time a new build is published, the url changes but it is expected that savings persists.
    We have other games made with a different engine (not Unity) and we are using a custom localSavePath to make savings persist among versions.

    Anyway, since the SaveList item seems to work, I can use it as a kind of Continue button. Is there a way to override label text? To display a different text (like for example "Continue") instead of "Autosave".

  • If you use Unity UI for the menu's display, you could hide the UI Button's Text object and display a separate one that shows "Continue".

    I think I see the issue with "Continue From Last Save" now, though.

    This option doesn't necessarily load the Autosave - only whichever was the last save to be created. AC will record which save this should be inside Options data, which - by default - is inside PlayerPrefs.

    The file location of OptionsData has its own handler - and an alternative that saves to system files is provided in the included OptionsFileHandler_SystemFile script, which can be assigned in a similar way to above, i.e.:

    Options.OptionsFileHandler = new OptionsFileHandler_SystemFile ();
    

    As before, you'd have to use a subclass of this that uses your custom filepath instead of Application.persistentDataPath.

    However, if you're only dealing with Autosaves, you don't need to rely on the "Continue From Last Save" method.

    In your Save: Save or load Action, setting the Method field to Load Game, you can then set the Save to load field to Autosave. This will simply load the Autosave, bypassing the need to read any Options data.

  • Thanks Chris, changed my action into Load -> Autosave and everything works like a charm.

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.