Skip to main content

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

How to persist data / save data in browser?

A topic by haretic. created Apr 24, 2023 Views: 423 Replies: 5
Viewing posts 1 to 4
(+1)

Love Decker! I haven’t been able to figure out how to make data persistent, if that’s possible. Would love to store some variables like a nickname a person chooses, or store the number of times they’ve run a timer, etc

Developer(+3)

Glad you're enjoying Decker!

If you save a deck (File -> Save), it preserves the contents of every widget and card. In some cases, this is all you need. This may be a bit inconvenient on the web, because web-decker cannot re-save decks in-place.

In a Lil script, you can use the "write[]" function to prompt the user to save images, sounds, text files, or arbitrary binary files, and later use the "read[]" function to request that the user choose a file to import. You could certainly use this functionality to add persistence to your decks, as well as interacting with an interesting range of existing file formats. See Built In Functions in the Decker reference manual for details.

Both of the above require some human intervention for saving/restoring data. I'm a bit hesitant to expose browser localStorage to web-decker, as it can be a very brittle place for storing information and it's unclear how to give native-decker functional parity. However, you could go rogue and tweak an .html build of your own decks to insert such functionality!

Starting from an .html deck, open it in your favorite text editor, search for a line that reads "primitives=(env,deck)=>{", and add the following to the function body:

 env.local('localstorage',lmnat(([k,v])=>{
  if(v){try{localStorage.setItem(ls(k),ls(v))                     }catch(e){};return v}
  else {try{const v=localStorage.getItem(ls(k));if(v)return lms(v)}catch(e){};return NONE}
 }))

This introduces a new built-in function to Lil, "localstorage[]", that can be called with one or two arguments, to read a localstorage key or write one, respectively:


(Naturally, this functionality won't work if your deck is loaded into native-decker, or some other instance of web-decker that does not contain the above modifications.)

Does any of that help?

(+1)

This is wicked awesome! Yes, it really helps. Thank you so much

do you think Decker will eventually integrate new browser FileSystem API features? So someday it can save in-place? 

Developer

Currently the outlook seems poor.

As I understand it, the current solutions are either Chromium-specific or Firefox-specific, with no Safari support whatsoever. I'm not interested in adding features that can only work in bleeding-edge browsers, especially if that browser is Chrome.

Worse, most new web APIs are gated behind the requirement for a "secure context", which demands that the page itself be served via HTTPS and not accessed from a local filesystem. This severely limits what I can use in a standalone single-document HTML build of Decker.

If, in the future, a portable API becomes broadly available for saving in place, I'd be delighted to add support.

(+1)

hmm, yeah. it seems like the new file system feature is not as useful as i was hoping it would be.