Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Any way to get localStorage (or other ways to preserve state) to work?

A topic by Alaric von Teplitz created Jun 01, 2022 Views: 915 Replies: 7
Viewing posts 1 to 8

I tried searching before deciding to ask this but found no definitive answer.

Is there a way to use localStorage? I'd like to save the user's preferences for playing music. Music can get pretty annoying, and so I have a toggle that turns it off. Unfortunately, being unable to store the state of the toggle, those who'd rather play silently are forced to turn it off every single time they load the game.

If there isn't a way to make it work with localStorage, what other methods do you guys use to preserve state between sessions?

Moderator moved this topic to General Development

Um, just use LocalStorage. It works.

What engine are you using?

(+1)

A custom one that I wrote. Calls to localStorage.setItem() or getItem() fail and crash the app (unless handled) once uploaded to Itch.

This is what I'm seeing if I try using it: 

This command worked in my gamepad mapper:

localStorage.setItem("gamepad_configuration", JSON.stringify(config_data));

The only thing that stands out is the capitalized 'Window'. If I go to the console and type "Window.localStorage" it shows undefined, if I type "window.localStorage" it shows a length zero object. If that's not the issue, it might be hard to debug without seeing the code.

The getter and setter look like this

localStorage.setItem('uvz-sound', JSON.stringify(G.soundEnabled));
JSON.parse(localStorage.getItem('uvz-sound'));

and

G.soundEnabled

is a Boolean.

(3 edits) (+1)

Oh, interesting, I checked in FireFox and it works just fine. Must be something about Edge, either the browser itself or some manual setting that I messed with and then forgot about. Works when developing locally, though, which is why I thought it was Itch's fault.

Edit: OMFG! Blocking 3rd party cookies is what's causing this!

(+1)

Just putting my two cents out here (from painful experience): you should always be prepared to fail gracefully when using local storage. There is no guarantee that it will be available. That depends on different security settings on different browsers on different platforms, along with the end users preferences.

Always assume local storage will cause an error.