How do I make LocalStorage work?

Get help using Construct 2

Post » Mon Dec 19, 2016 3:50 pm

Hello, afaik LocalStorage is rather used than WebStorage, right? I am a bit confused because the following lines don't work at all:
http://prntscr.com/dlcjet
the gamescore doesn't set either case..am I missing smthg?
B
12
S
3
Posts: 211
Reputation: 1,477

Post » Mon Dec 19, 2016 4:06 pm

You need to run the "Check item exists" action at the start of your layout for your triggers to work. According to the manual:

"Check if a key exists in storage. This triggers either On item exists if the key exists, or On item missing if the key does not exist. If the item exists, the ItemValue expression is set to the key value in the On item exists trigger, so there is no need to use a subsequent Get item action to read the value."

Read more about LocalStorage here: https://www.scirra.com/manual/188/local-storage
B
128
S
33
G
17
Posts: 1,551
Reputation: 20,488

Post » Mon Dec 19, 2016 4:12 pm

oh, i completely missed that! thank you
B
12
S
3
Posts: 211
Reputation: 1,477

Post » Thu Jan 19, 2017 1:40 am

But when do not exist, I want to create it, how do I do it?

Because the "else" is not supported.
B
7
S
2
Posts: 9
Reputation: 493

Post » Thu Jan 19, 2017 1:49 am

On item missing
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Thu Jan 19, 2017 1:55 am

josevasquez wrote:But when do not exist, I want to create it, how do I do it?

Because the "else" is not supported.



It will 'exist' after you set a trigger for it to exist.

Example:

Player - On destroyed > System - Set lives (lives is the global variable) to 4

Then add the action to set lives to localstorage:

Localstorage - set lives_key(1) to lives(2)

Number 1, the lives_key is just a name for THAT data in localstorage. You can call it anything you want, but most people will use the global variable its saving and some will add the KEY after it, just as a quick reference so they don't, i mean me, get confused.

So when you start the game again, that localstorage WILL exist now, and it will have the value of 4.
B
38
S
18
G
19
Posts: 664
Reputation: 14,107

Post » Sat Jan 21, 2017 8:17 am

Thank you very much to both. I used "on item missing", and solved my problem.
B
7
S
2
Posts: 9
Reputation: 493


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 5 guests