Jump to content

Checking if Local Storage is empty


Ninjadoodle
 Share

Recommended Posts

Hi guys

 

I understand how to setup Local Storage and how to store values in it.

 

I'm trying to save level progress, but need to check whether the Local Storage is empty on the first play, in order to send the player to the first level.

 

1. Is this the correct way to do this and will this work across multiple browsers?

2. This doesn't seem to work with CocoonJS

3. Also, could somebody please explain to me, what the zero after 'level' stands for?

 

if (game.storage.get('level', 0) == null) {

    game.system.setScene('Level1');

} else {

    game.system.setScene(game.storage.get('level', 0));

}

 

Thank you heaps in advance!

Link to comment
Share on other sites

In the Panda.js API the 2nd parameter is the default value as per http://www.pandajs.net/docs/classes/game.Storage.html#method_get

The proper way to check for the existence of a key would seem to be to use the has() method - asking the get method to return the number 0 if a key is not found and comparing this to null with == seems a little wacky to me :|

(Actually, calling get() again after the above test with the same parameters seems wacky to me too, as does then blindly using the value without further range checking).

Your code also doesn't do what you describe - rather than checking that local storage is empty - it merely checks for a specific key. If you want to check if it is empty it looks like you might have to bypass Panda's API and check localStorage.length === 0, but that seems a bit ugly to me especially if you feel you might want to extend the system yourself later, for example to store the data using some obsfucation...

Link to comment
Share on other sites

I'm not sure if this is the right method or not, but I use the parseInt function that parses a string and returns an integer:

currentLevel = parseInt(game.storage.get('currentLevel')) || 0;

if currentLevel is not an integer (cause doesn't exists), it will be set to "0". Than select the level based on currentLevel value.

 

 

I used this way also for initial settings parameters and as trick to show the tutorial at first game start.

Link to comment
Share on other sites

Thank you chg and Phempt :)

 

I've only just looked into local storage and I'm sure I'm getting it completely wrong.

 

Phempt - Would this be the correct way of using your technique? 

currentLevel = parseInt(game.storage.get('currentLevel')) || 0;if (currentLevel == 0) {    game.system.setScene('level1');} else {    game.system.setScene(currentLevel);}

One thing I'm still wondering tho, is whether it's possible to get this working with Cocoon JS or what the alternative would be.

 

Thank you again for the help!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...