#Does anyone know how to properly save with PlayerPrefs in the html version?

1 messages · Page 1 of 1 (latest)

valid rampart
#

Ran into a problem when I was testing an html version of my project where I realized that if you just left the page your progress wouldnt be saved; you would have to specifically hit quit instead. This worked in my windows version, but I just cant manage to get it right here.

I went through a lot of forums and documentation, and as far as I can see it seems like PlayerPrefs are supposedly saved whenever you run Application.Quit(), but I can't find any way on how to save them otherwise except for using PlayerPrefs.Save(), which I've already been doing for quitting and regular saving. Don't really know how to solve this, any help would be appreciated.

safe imp
stiff flint
#

You could probably add extra layer of sureness by adding JS interop to get callback if they try to close window / tab

#

could call the Save method there too or let them choose if they wanted to do so before closing

valid rampart
# stiff flint could call the Save method there too or let them choose if they wanted to do so ...

yeah naw sorry I was a lazy bastard in terms of responding but basically I was always using the save function every time I saved and so I got confused as hell on if the function was supposed to actually save my prefs or prepare them to be saved upon proper close function.

Still fairly helpful tho, as I'm now certain that it's definitely something specific about the structure of my horribly bloated saving code lmao

tulip condor
#

@valid rampart you may need something like:

#if !UNITY_EDITOR && UNITY_WEBGL
internal static class WebGLHelper {
    [System.Runtime.InteropServices.DllImport("__Internal")] public static extern void SyncDB();
}
#endif

and this in Plugins

const FileIO = 
{      
  SyncDB: function()
  {
    FS.syncfs(function (err) 
    {
        if (err)
        {
          console.log("[FS Database] Complete: " + err);
        }
        else
        {
            console.log("[FS Database] Complete");
        }
    });
  }
};

mergeInto(LibraryManager.library, FileIO);

And call WebGLHelper.SyncDB() after you save data.

#

Why? I don't remember. I needed it for some of my saves to work, maybe it'll help 🤷‍♂️