Mess up fixed


TIL. 

In Unity, if you use the default path `Application.persistentDataPath` and then upload to itch, then whatever you save in the persistentDataPath will remain there only till you upload the new build. Afterwards that all is gone because the persistent data path changes with each build upload. 

To fix that you have got to create your own, truly persistent path. A very nice post on the topic: https://ddmeow.net/en/game-dev/save-persistent-itch-io/. Long story short, you have to make your own path to save the file in indexed database

public static class PersistanceStorage {
     private static string mPersistentDataPath;
     static PersistanceStorage()
     { 
 #if UNITY_WEBGL
         mPersistentDataPath = "idbfs/Mathemando-Little-Cool-Puzzle-randomhash-423";
         Debug.Log($"[PrefsStorage] Using WebGL persistent path: {mPersistentDataPath}");
 #else
         mPersistentDataPath = Application.persistentDataPath;
 #endif
         if (!Directory.Exists(mPersistentDataPath))
         {
             Debug.Log($"[PrefsStorage] Directory does not exist. Creating directory: {mPersistentDataPath}");
             Directory.CreateDirectory(mPersistentDataPath);
         }
         else
         {
             Debug.Log($"[PrefsStorage] Directory already exists: {mPersistentDataPath}");
         }
     }
// ... your persistence logic


Using PlayerPrefs has the same issue, so all the player prefs have to also go into the real storage.

But that's not all. I also noticed that storing data did not happen immediately. Sometimes my data got updated and sometimes even after some minutes of play it got reset to the previous state upon browser reload. So we've got to save the changes to the file system after modifying the files. Got the answer how to properly do it here:

https://discussions.unity.com/t/system-io-file-doesnt-work-properly-on-webgl-pla...

 #if UNITY_WEBGL
     Application.ExternalEval("_JS_FileSystem_Sync();");
 #endif


A learning from that: if you have persistence, have a second "shadow" project and test your releases there first before touching the main release. Because if you have a lot of players they will have.. a lot of disappointment! Not my case though :D 

zero bugs


In the end, I also decided to put the fix with more code on Hashnode just to have it in a more readable format. Check it out there if you like 

https://pavelstepanov.hashnode.dev/saving-game-data-in-webgl-when-using-unity


------


And, to the other news. The new difficulty system is out there. Now if you are bored or, on the contrary, want to have a more relaxed gameplay, you can play around with difficulty and find what suits you.

Files

Mathemando.zip Play in browser
4 days ago

Leave a comment

Log in with itch.io to leave a comment.