DDmeow » Game-Dev » How To Make Your Save Persistent Through Different Builds At itch.io (WebGL)

How To Make Your Save Persistent Through Different Builds At itch.io (WebGL)

by | 2021-06-27 | Game Dev Notes By Noob, Game-Dev

Follow Us!

Follow (KS) Follow (Steam) Subscribe (Youtube) Follow (Twitter) Follow (ig)

Save Persistent Path Problem

It is taught that Unity’s “Application.persistentDataPath” will persist through different builds. That’s the case for desktop builds. However, if you’re uploading your builds to itch.io (or maybe some other platforms too), the URL that host the build will change every time you upload a new build. Hence, “Application.persistentDataPath” will change every time the game is updated. And the old save will be gone.

Solution To Save Persistent Path Problem

To solve that, use an absolute path for all your builds. Replace “Application.persistentDataPath” with “idbfs/YOUR_GAME_RANDOM_CODE”. I’d add some random codes like “sgwhf94hgfw” at the end just in case someone else is making a game with the same name and using the same method for saving.

PersistentDataPath returns different paths for different builds

☝ I actually found the answer here first then added my own solutions to it when more problems popped up.

Create The Path

As you’re using your own defined path, Unity won’t create the path for you, so you’ll have to generate the path yourself. Just put this before you use the path:

if (!Directory.Exists(savePathName))
{
Directory.CreateDirectory(savePathName);
}

Additional Note Of WebGL save

The save file is saved in the player’s browser. So,

  1. The save will be gone if the player clears the browser’s data.
  2. If the player switch browser or computer, the old save will not be there.

Related To Saving:

Code Monkey’s Simple Saving and Loading with JSON to a File (Unity Tutorial for Beginners)

5 Comments

  1. Anonymous

    Could you extend the tutorial on how to actually write to that persistent path? Using File.WriteAllText or FileWriter does create the files, but only for runtime. As soon as I’m reloading the game the save game is gone.

  2. Anonymous

    Hi, it seems like my save file is created on itch.io, but only exists at runtime. If I reload the page it’s not there anymore. How can I make it keep existing even if I close the browser? Thanks in advance 🙂

    • S.Lam

      What method did you use to save?

  3. BB

    Hi there, I read your article on making saves persistent in webgl on itch but I have a question that I can’t find an answer for anywhere. HOW do I actually change the application.persistentDataPath? The API says it’s read only and I can’t find it anywhere I could actually change it. Thank you for your help!

    • S.Lamb

      Are you talking about player pref? If it’s player pref, I’m not sure if it can be changed.
      For me, I am using a custom code for saving. The saving code should define the path of the save file. You can look for tutorials online on how to write the code for saving:

      https://www.youtube.com/results?search_query=unity+save+and+load+

Subscribe

To Receive DDmeow's Latest News on Games and Animations!

I accept the privacy policy 我同意此站的私隱政策

You have Successfully Subscribed!