How do I save the game progress in WebGL?

Hello, such a problem: I have a game that was developed for the Web, as in any game I made saves via PlayerPrefs, everything works as it should in Unity itself, but when I upload the game to the site(itch.io) and I start testing there, then nothing is saved and after rebooting everything is reset. What is the reason for this ? how can I solve this problem ?

Are you calling PlayerPrefs.Save at any point? On WebGL OnApplicationQuit is not implemented and therefore Unity will not automatically save PlayerPrefs when quitting the game.

1 Like

If you look at the official documents on PlayerPrefs, you’ll see it is supported in WebGL by using IndexedDB.

Do check what @PraetorBlue says, but I would also caution a few things.

While most browsers latest version do support IndexedDB, someone on an older version may not have this support.
I’m not sure if IndexedDB works in private mode?
PlayerPrefs is still, from previous experiences, not the best experience on WebGL. I have had it both work and then not work later on.
I’m not certain if IndexedDB can be cleared easily if someone were clearing their browser cache or history or something like that.

My suggestion is look into a backend service where you can save your data. If you don’t want to pay for one because your game is not expected to be that large, there are some that are free or offer a generous amount of data use for free per month.

Yes, I wrote a timer that is in Update and every 5 seconds PlayerPrefs is called. Save

    private void Update()
    {
        if (timer > 0) timer -= Time.deltaTime;
        if (timer < 0)
        {
            timer = 0;
            SaveGame();
            PlayerPrefs.Save();
            timer = 5;
        }
    }

I’m a beginner, I don’t know how correct this solution is :slight_smile:

I’m just learning, this is my first game, I really don’t want to mess with the server :slight_smile:

In truth, you might not have a choice when building for the web. I just haven’t seen PlayerPrefs be reliable in a web build for something as important as save game data.

Then can you tell me how to implement it through the server? maybe there is some kind of documentation or video?

As I mentioned, a backend service is probably the best way to go. I don’t have a direct answer for you, because it depends on what you go with. We have some we use here at work, but they may not be the best option for you. I know Epic games has a backend service. They have a Unity SDK as well, but I haven’t looked at it’s integration yet to know how well it works. But if you find one that you are interested in, they will have videos and tutorials on how to setup to save and load from them.

thanks for the help

Sorry to revive an old thread, but this was the first result in Google.

If you have your own hosting solution, PlayerPrefs and other saving methods should work fine. The problem is with other services, like itch.io or facebook. Every time you upload the latest build, they put the game in a different location. While this should be fine, it actually causes Application.persistentDataPath to generate a new directory with a new hash, making the old data essentially lost.(PlayerPrefs and persistenDataPath point to the same place)

I think the problem is how Unity generates that folder. Yes it makes it unique, but it doesn’t play nice with hosting services like itch.io and facebook. This really messes things up when performing updates to your game, as even PlayerPrefs break.

The solution is to avoid using PlayerPrefs and Application.persistenDataPath and save to the path “idbfs/GAME_NAME_PLUS_UNIQUE_HASH” instead. You have to create this path and replace GAME_NAME_PLUS_UNIQUE_HASH with something unique of course.

7 Likes

Unfortunately, this isn’t a viable option as the browser gives the warning “UnauthorizedAccessException”. You might have to use your own server to store the data possibly :frowning:

The data is not stored on any server, it is stored locally on the users machine. Using your own server to store the data would be the best, but it’s not something I would want to maintain and shouldn’t be necessary.(needs a user database, authentication and security.)

Please start a new thread in the WebGL part of the forums, so as to not hijack this one with a different problem than the OP’s. As I can tell you that the solution provided works, something else is getting in the way.

as far as I noticed there is a problem with the security of saving files directly on your machine when you are in WEBGL Build. not a problem with regular builds because it allows you to save now to your system but for WebGL, you need a host for your data downloading it from there, with
the workaround of addressable objects.
I know unity made it hard to save and load via WebGL builds because of the restricions of WebGL. I hope the process will be simplified like in python.

Unity didn’t made it “hard” at all. The file system restrictions are not made by Unity but by browsers security policies. Most browsers have even stricter security rules for html files loaded from the local file system. In the past browsers simply allowed read access to other files in the same directory in order to run a website locally. However this is of course a huge security risk if you download an html file into your downloads folder and when opening the file locally, it would have access to all your files in your downloads folder. That’s why most browsers now don’t allow even read access to any other files when opening a html file locally.

Though for properly hosted content, browsers offer the IndexedDB API which Unity uses to wrap the System.IO methods using the “/idbfs/” filesystem. So you can happily create “folders” and “files” in the persistentDataPath as you could on a local machine. Just keep in mind that the memory is usually restricted, though that’s usually not an issue.

3 Likes

I agree that unity is facing the security issue of Browsers WebGL. so yes you are right it’s not unity’s fault. but the problem is the hash code that when you update a build. then the hash code is in a different location, some sites like itch.io and others remember the old path creating havoc.

unity should create a free utility for saving and loading via WebGL game progress built into the IDE.
so developers will not waste time on it. losing all their creative ideas on the way. just saying.

2 Likes

Well, that’s an issue with those sites specifically and not really with Unity. If the URL is different it’s a different game / file. Everything web related is bound to the URL or domain name, even cookies. If the path for which a cookie was set changes, the cookie would be lost as well.

Note: If data would only be stored based on the domain name, it would mean that all itch.io games would share the same virtual file system. Unity specitically uses the hash of the URL to distinguish several different games on the same hoster. If the hoster (like itch) decides to produce random paths / file names, you could not distinguish a “completely seperate game” from “a new version”. The only alternative would be to use your own unique ID as mentioned by Chris or to use your own server. If you don’t need server side scripting, you can simply host your game on github pages. Here’s my example page.

what about unity will hold it in their play.unity.com like git? with a link to the project? with unity version control not like plastic that lags all the IDE?

BTW
thank you very much it was very helpful. I just used gist to store json files. thanks.

…
for example here is something I did to go to different links of my other stuff. it was working until itch.io and unity denied accesses of each other. unity allow the internal links to work but itch.io blocked all.

the same build at itch.io:
https://yodda.itch.io/yoddatown

and at play.unity.com:
https://play.unity.com/mg/other/yodda-town

If you configure a PHP and MySQL database, on an external server. You can recover the data in your games. Useful for example if you needed to display a hightscore of your games.

1 Like