+3
Fixed

Saved Variables Not Persisting On WebGL

Dennis Murphy 3 months ago updated by Rudolph Thomas 2 weeks ago 16

Hello there encountering some issues with saved variables in my WebGL build and hoping someone can help!

The issue is very simple and is reproducable in a barebones project (happy to provide a zip if needed). Any time i change a saved variable in my WebGL build it is never persisted on page refresh. I created two simple projects which increment/decrement a saved variable, one using Bolt and one using a MonoBehaviour script. They both work as expected on every platform I've tried (standalone, Android, in-editor) except WebGL, where the bolt version fails to persist the value, but the MonoBehaviour version succeeds. I've been sure to run AOT Pre-Build before my WebGL build with no difference in behavior.

Here is my bolt graph for setting the variable:



I even attempted an explicit call to PlayerPrefs.Save() after the SetVariable call to no avail. My guess would be the issue is either the saving code inside of Bolt, or perhaps the code which detects and resets saved variables to their initial value erroneously firing.

Thanks in advance for any help!
- Dennis

Bolt Version:
1.4.5
Unity Version:
2019.1.10f1
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

I Am also at the verge of implementing a WebGL app making use of this functionality. So far I'm testing on Android and Windows Desktop Builds. 

I Wonder if it could have something to do with:
https://forum.unity.com/threads/playerprefs-doesnt-save-in-webgl.650935/
https://issuetracker.unity3d.com/issues/playerprefs-dont-get-saved-in-a-webgl-build

Thanks for the links! I wasn't aware of this issue on the Unity end. Unfortunately it doesn't seem to work using Bolt on either of the following versions:

  • 2019.2.0b9
  • 2019.3.0a8


Pitty if that is the case. I'll report back with my findings when I get to that portion of my project. Are you on Discord. I'm going to post your topic there to ask for advice because this is important for me too.

Yes i am on the discord as well @DennisMurphy. Have you been able to verify the issue as well? It'd be nice to know if it's this easily reproducible.

Busy with something to test

My Initial testing shows that it fails:

1. Debug Run in Unity works as expected.
2. WebGL Build runs but Saved variables are not persisted.
    I Did test it in Chrome and Firefox on locally installed WAMP server running on 127.0.0.1

Compiling and Running on my Android device works perfectly. Seems to be an "issue" with WebGL

+2
Working on Fix

Hi everyone,

Thanks for the reports.

It seems that WebGL does not receive the OnApplicationQuit event, which is the hook that Bolt uses to save the variables.

Furthermore, calling PlayerPrefs.Save() manually in your graph won't be enough, because the variables are not yet serialized at this point.

I will look into exposing a Save method in a future version.

In the mean time, you can create a simple wrapper script like so:

using Ludiq;
using Bolt;

[IncludeInSettings(true)]
public static class WGLSaveFix
{
    public static void Save()
    {
        SavedVariables.SaveDeclarations(SavedVariables.merged);
    }
}

Then, rebuild your unit options and you should see a WGL Save Fix: Save unit appear in the fuzzy finder.

Use that unit at any point you want to save your variables. This can either be after a specific action, or you could put it in a looped coroutine to save at a regular interval. It's up to you!

+1

Thanks for the work around Lazlo!

Small note, if you're trying to use the workaround above you'll also need to include a "using Ludiq;" at the top so you can use the IncludeInSettings attribute!

+1

Adding a Save Variables unit in v.1.4.6 to let you manually save variables on WebGL without requiring the workaround script I posted earlier.

Here's a very simple example setup to save every 30 seconds:

As posted on Discord

I'm having an issue I want confirm with WebGL builds where saved variables are not persisted. @Lazlo patched it by adding a Save Variables block. In his example he is showing how to save the Saved variables in 30 sec intervals in a coroutine.

In my example below I found that I have to add an "End of Frame" block and a coroutine check on all the events where I would like the saved variables to be serialized and saved.



Has anyone else dealt with this before?

Another interesting bit is that if I use a Wait for Next frame the UI input fields hang up but selectors are still functioning.This can only be seen when compiling and running the WebGL code on a server.

I'm now on unity 2019.2 and using Bolt v.1.4.7 latest

Forgot to add the reason why I'm reporting this. If I don't do it this way the saving of values is always one step behind. Example:

Assume I'm saving everytime a character is typed in a text box.
Test 1

1. Type one Character
2. Quit and reload WebGL
3. Character not saved
Test 2

1. Type two characters example XY
2. Quit and reload WebGL
3. Only X is retained and Y did not get saved

I Tried using two "Save Variables" in sequence but dit not work

This is how I ended up experimenting with End of Frame and using Coroutines as in the example above.