Default Values for Persistent Data

Hey yall!
I’m planning to make a game that has a few variables that persist across restarts/playthroughs but I wasn’t sure how to go about setting the initial value. Maybe I’m overthinking it. I’m not sure how to check if there is an old value to load or if I even need to. Theoretically my logic would be something like

if variable has old value then
  restore variable
else
  variable = "some default value"
end

I’m not sure I’m answering the exact question you have, but in my games, the pattern I’ve used has been:

[game] script

on load do
	// Init
	config.inputRepeatDelay = 0.3
	config.inputRepeatBetween = 0.2
	
	keysCollected = 0
	keysTotal = 6

	gotSword = 0
	gotGold = 0
	gotCrown = 0
	gotHat = 0

	restore
…

So, I set all the default values on load, then call restore. If there’s something stored, it’ll overwrite these values; if there’s not, then the values I just gave will hold. Does that make sense?

Absolutely! I just assumed restore would always provide a value, my mistake. In the case of restoring a specific variable using the restore varName syntax, does it not override it if it doesn’t exist?

Good question, even I had to look this one up! :sweat_smile: restore “varName" returns 0 (the default value of an uninitialized variable) if there is no variable by that name in the store.

[EDIT: some more reading in the docs and i see now that Pulp data doesn't save when i call "store", but rather is queued up for a write to disk when i next transition between rooms? that's my issue then - i've created a single room and randomize it each time the play finds my "door" tile, which is not a "pulp exit" tile because i don't link to another room

i'll try dropping to a phantom room between respawns to see if that helps]

I'm struggling with store/restore here myself.
first question: does the virtual web-based play.date support the feature? if not, that's my issue and i can happily wait till i have hardware or the virtual runtime is updated.

a quick summary of my environment:

  1. i've started a random dungeon crawl game; for now it just creates a dungeon covered in fog and reveals legit paths as you walk near or on them. if you reach the exit it respawns in a newly randomized room full of hidden paths and chambers
  2. experimenting with store/restore i added a step counter and a "record" variable; the goal is to reach the exit in the fewest steps
  3. upon reaching the exit i update the "record" variable (if you bested your prior attempts), then "store" the record
  4. in my "game" script i explicitly restore, then check if record==0. if so, i set record=999 so i can beat it in the future.

my issue:

  1. i do the above and everything works great aside from store/restore
  2. the code which stores my record is instrumented with some log statements and i see those execute
  3. if i refresh the browser my "record" gets set back to my default value of 999

any ideas out there?

I'm apparently too new here to upload attachments... i can share via google drive or similar if that helps. my little WIP game is called Loot'n'Scoot :slight_smile: