Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / [SOLVED] HTML5 Shared Variables, Feature Request to access localStorage

Author
Message
Prime
User Offline
Joined: 15th Jun 2022
Location:
Posted: 25th Jul 2022 17:45
I'm struggling with the Html5 shared variable functions SaveSharedVariable() LoadSharedVariable() DeleteSharedVariable(). I exported the sample at https://www.appgamekit.com/documentation/Reference/Extras/SaveSharedVariable.htm but the results are

Word: Failed
Number: -1

indicating that the variables are not getting saved and loaded.

The help pages say Html5 shared variables are stored as cookies, which have pretty severe size limits. Is there a way to use localStorage (https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)?

How are you all saving player data like saved games in Html5?

The author of this post has marked a post as an answer.

Go to answer

Virtual Nomad
Moderator
16
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 25th Jul 2022 18:54 Edited at: 25th Jul 2022 19:08
This post has been marked by the post author as the answer.
when hosting games on itch.io, i apply this fix which would apply to you if you're using Cross-site cookies like itch does. otherwise this issue is known.

then, this request may be the same as yours? i'm not too savvy with terminology but if it isn't, please add the request to the repo?
Prime
User Offline
Joined: 15th Jun 2022
Location:
Posted: 26th Jul 2022 21:27
Worked like a charm.
Prime
User Offline
Joined: 15th Jun 2022
Location:
Posted: 27th Jul 2022 01:08
Data in cookies is limited to 4KB and localStorage can be used instead to store larger data that persists between sessions.

I have tested a modification to AGKPlayer.js to allow access to localStorage

unminify, then replace



with



as you can see, any shared variable starting with _ls will then be redirected to localStorage, so you might have functions like these



to use files normally or localStorage if you export to Html5
Virtual Nomad
Moderator
16
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 27th Jul 2022 01:27 Edited at: 27th Jul 2022 01:29
Quote: "access to localStorage"

very nice.

if you're confident in the code, consider making a separate post so that it gets the attention it deserves?

and, i expect the data is saved in the WritePath() ?

i'm sure it can be further modified to load/save to/from localStorage OR as a cookie? is there any advantage to either?
Prime
User Offline
Joined: 15th Jun 2022
Location:
Posted: 27th Jul 2022 02:42
Quote: "and, i expect the data is saved in the WritePath() ?"


localStorage data is saved in the browser, ultimately in a file somewhere on the client side but I couldn't tell you where. You can view and modify your own localStorage values from within the browser.

In https://itch.io/t/186563/html5-game-save-data-locally an Admin at itch.io writes "You can use local storage to save data. We currently have an issue where uploading a new build of your game changes the path it lives on, so you might lose your storage depending on how you've saved it."

I can write and read back variables from localStorage, and find them in the browser, my tests uploading a new build on itch.io and gamejolt.net don't lose the saved data. The note at itch.io is from a few years ago, and doesn't seem to apply (at least not all the time) anymore. I'm a little out of my depth writing about localStorage as it's something I've used before but not dealing directly with javascript.

I'm not as comfortable as I'd like to be with how it works. I'm pretty sure it won't crash, but the duration of localStorage by browser and by hosting site might vary. I'll make a new post so more people will see it.
Prime
User Offline
Joined: 15th Jun 2022
Location:
Posted: 27th Jul 2022 03:00
Quote: "i'm sure it can be further modified to load/save to/from localStorage OR as a cookie? is there any advantage to either?"


As it is, variables with names that start with _ls will be stored only in localStorage (and not as a cookie), everything else (except the variable "args") will be stored as a cookie. I'm not the best person to write about this, but cookies are limited in size (4KB, some of which will be taken up by variables from your hosting site) and I think they're passed back and forth from the browser to the server quite a bit but I don't know how often. localStorage gives you 5MB and I think the server should never see it.
Virtual Nomad
Moderator
16
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 27th Jul 2022 03:45
thanks for that.

i know there's so much like this ready to be unlocked for AppGameKit re: HTML and appreciate you and a few others that turn the key for us.

Login to post a reply

Server time is: 2022-08-09 08:09:13
Your offset time is: 2022-08-09 08:09:13