Page 1 of 1

Tease storage: saving score for next visit to same tease

Posted: Thu Oct 28, 2021 11:46 am
by jonasty01
Hi there

I,m making a tease in wich you have to increase score on every page to unlock a next page.
I have no clue on how to save the score for the next visit.

So far I manage to add it up over pages, but I dont manage to save it on a next visit.
I first put it in init script: score == 0 . But that makes the score reset on every visit right?
I've tried to put the save variables tutorial into my tease, but that doesnt seem to work either...

Basic idea: you can navigate to next or previous page and with the "if" boolean (if score>= ..) i determine if they can acces the page.

Hope you can help :/

Re: Tease storage: saving score for next visit to same tease

Posted: Thu Oct 28, 2021 4:52 pm
by schefflera0101
Hi jonasty01,

You need to enable storage. Next write an init-code via:

var score = teaseStorage.getItem("score") || 0

This loads the score you have saved last time.
To save a score, you need to write an eval:

score=5
teaseStorage.setItem("score", score)

(instead of score=5 you can also write score=score+1 (adds 1 to your score))

I hope it helps you.

Re: Tease storage: saving score for next visit to same tease

Posted: Fri Oct 29, 2021 1:44 am
by Roblsforbobls
schefflera0101 wrote: Thu Oct 28, 2021 4:52 pm (instead of score=5 you can also write score=score+1 (adds 1 to your score))
I think my favorite way to add or subtract one is using score++ and score--. Same thing as score = score + 1, but it's faster to type

Re: Tease storage: saving score for next visit to same tease

Posted: Fri Oct 29, 2021 6:02 am
by bbman
I generally prefer a little more abstraction in the more tease specific parts, which can easily be obtained by wrapping your score storage within a player object:

Code: Select all

let player = {
    set score(value) {
        teaseStorage.setItem("score", value);
    },
    get score() {
        return teaseStorage.getItem("score") || 0;
    }
};
This allows you to write simple and readable code in the more important part of your tease logic, like below, and have your score saved across sessions automatically without having this save/load logic mixed in with the flow of your tease.

Examples:

Code: Select all

if (player.score >= nextPage.requiredScore) 
// unlock next page.
or

Code: Select all

if (playerAnswer == A)
    player.score++;
else if (playerAnswer == B)
    player.score = 0;
else if (playerAnswer == C)
    player.score -= 5;