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 :/
Tease storage: saving score for next visit to same tease
- schefflera0101
- Explorer At Heart
- Posts: 124
- Joined: Wed Oct 07, 2020 12:21 pm
- Gender: Male
- Sexual Orientation: Open to new ideas!
- I am a: Submissive
Re: Tease storage: saving score for next visit to same tease
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.
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.
-
- Explorer At Heart
- Posts: 264
- Joined: Tue May 21, 2019 2:27 am
- Gender: Male
- Sexual Orientation: Asexual
- I am a: Switch
Re: Tease storage: saving score for next visit to same tease
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 typeschefflera0101 wrote: ↑Thu Oct 28, 2021 4:52 pm (instead of score=5 you can also write score=score+1 (adds 1 to your score))
Re: Tease storage: saving score for next visit to same tease
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:
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:
or
Code: Select all
let player = {
set score(value) {
teaseStorage.setItem("score", value);
},
get score() {
return teaseStorage.getItem("score") || 0;
}
};
Examples:
Code: Select all
if (player.score >= nextPage.requiredScore)
// unlock next page.
Code: Select all
if (playerAnswer == A)
player.score++;
else if (playerAnswer == B)
player.score = 0;
else if (playerAnswer == C)
player.score -= 5;
Who is online
Users browsing this forum: No registered users and 3 guests