Heppell08

localStorage Question/Help

Recommended Posts

i've just started adding my gamesave feature and ran into a bit of a snag.

 

I have a pretty specific way of destroying and reloading a map and in that function i am autosaving as it loads each level. So when i load EG: map1 i set the item in storage for that level.

 

What my problem is, is that i want to have the map loaded from storage from my main menu (which i got working fine) and then have start game clear any storage if it exists. So basically, start a new game, clear the storage and load map, then in another button get the storage and load accordingly. Now i have it setting items per map like so:

localStorage.setItem('user', 'level01');// level 1localStorage.setItem('user', 'level02');//level2// the 'user' never changes

Then in my main menu start a new game button, i have this:

 

startGame: function (activePointer) {menuMusic.stop();localStorage.removeItem('user');this.game.state.start('Story');},

But for some reason the storage isnt wiped and its loading the saved data irregardless of what i put there. 

The reason i want to wipe on the button is because when im loading a new map i've since added this:

if(mapnumber === 1 || localStorage.getItem('user')){// load map here etc etc }

So if there is no storage there it will load from the mapnumber but its loading from the storage. Any ideas what i can do?

 

 

Share this post


Link to post
Share on other sites

So i started using store.js for local storage and then added this into the loading of my map at the top:

loadGame: function()        {           var gameSave = store.get('user');           if(gameSave)           {                store.get('user');           }           else            return;        },

I'm using that to check there is locally stored data but still not getting the right results. I save at map 3. load the game and still get reset to map 1...

Share this post


Link to post
Share on other sites

The thing to remember about localStorage is that key-value pairs stay that way until they are overwritten. If you are setting a value earlier in your code, or even from a previous session, that value will be there later. And it will continue existing until removed or overwritten at some later point too.

 

Since you seem to be using store.js, have you tried calling store.clear()? When I was testing some code that was using localStorage in the past, I ran into a similar problem. Making sure I had cleared all the keys each time I was testing prevented a nasty error I was having where old values would show up future sessions.

 

Something else to try is the following:

store.forEach(function(key, val) {    console.log(key, '==', val)})

Just test that the values you expect to be there are actually there.

 

Oh, and remember that localStorage is accessible from nearly all JavaScript running in the browser. It is often a good practice to add some type of identifier or name as part of the key. Just "user" can be useful in testing, but to make sure you aren't running into other keys called "user" from another source, adding something like your or the project's name before it can help. Reverse domain naming is very common.

Share this post


Link to post
Share on other sites

i have the key set to user and i have a seperate key per level like so:

 

saveGame: function()        {            if(mapnumber === 1)            {                store.set('user', 'level01')            }            if(mapnumber === 2)            {                store.set('user', 'level02')            }            if(mapnumber === 3)            {                store.set('user', 'level03')            }            if(mapnumber === 4)            {                store.set('user', 'level04')            }            if(mapnumber === 5)            {                store.set('user', 'level05')            }            if(mapnumber === 6)            {                store.set('user', 'level06')            }            if(mapnumber === 7)            {                store.set('user', 'level07')            }            console.log('SAVED');        },

that is per map as the code above shows. The problem is that it doesnt load the map i want. I'm also using a lot of console logs to tell me that the user key and identifier match prior to setting up the load game function. It just isnt loading the map i want for the save ive given. 

 

Share this post


Link to post
Share on other sites

So, with just--

store.set('user', 'level01')

--you aren't getting back 'level01' when using store.get('user')?

 

This may read as obvious, but what does the following produce?

console.log(store.get('user'))

When tested right after, does it return the correct value?

 

Something like the following, maybe.

saveGame: function()        {            if(mapnumber === 1)            {                store.set('user', 'level01')            }            ...            console.log('SAVED');            console.log(store.get('user'));        },

Share this post


Link to post
Share on other sites

It returns with the value you'd expect. I get a return of it being level01 and user. That's what is confusing me. I removed the autosave that was overwriting previous saves(as expected) and created an S key addKey function an pinned the savegame function to it. I get the results I want from the console log being the level required, just when I load it resets yet I don't setItem any code. I load, save and destroy ONLY if I tell it to do so ingame and not in any automatic sense at all.

Share this post


Link to post
Share on other sites

It returns with the value you'd expect. I get a return of it being level01 and user. That's what is confusing me. I removed the autosave that was overwriting previous saves(as expected) and created an S key addKey function an pinned the savegame function to it. I get the results I want from the console log being the level required, just when I load it resets yet I don't setItem any code.

 

What I think might be happening is the values are being saved across sessions.

 

I load, save and destroy ONLY if I tell it to do so ingame and not in any automatic sense at all.

 

Exactly.

 

That's what can be so frustrating about testing with localStorage. It retains the values across sessions and even after the browser is closed too.

 

Something to try, like I ended up doing myself, is using store.clear() once every test to make sure nothing is being saved between one test to another. That can often save you from having old values saved when you don't want them to be.

Share this post


Link to post
Share on other sites

Yeah I knew that too. That's why I had a button have a clear on any localStorage so that I could save a 'fresh' piece of data.

Used the clear and still having problems lol. Feel like I've covered the bases of the localStorage yet still failing somewhere else in the code. As a run down I've done this to try saving:

Bound S key to save function with a user key and level name string.

I've created a clear function to empty any storage already stored so that I can test functionality.

I've console logged the strings saved data and getting the results required but its the loading that fails.

I use states and not sure if that is/or having any effect?

The variables are global and the functions work fine (logged in console) so I'm totally lost on what's going on.

Share this post


Link to post
Share on other sites

Ok i got this working in i believe to be a strange but wonderful way without store.js. Here goes what i did:

 

First off in my savegame function i did this:

 

saveGame: function()        {            var gameSave = {};            gameSave.mapnumber = mapnumber;            localStorage.setItem('user', JSON.stringify(gameSave));            console.log(JSON.parse(localStorage.getItem('user')));        },

Then in the top of my level selection code i have this:

 

createMap: function()            {            var gameSave = JSON.parse(localStorage.getItem('user'));            if(gameSave)            {                console.log('LOADED!!');             //   JSON.parse(localStorage.getItem('user'));                mapnumber = gameSave.mapnumber - 1.5;                console.log(JSON.parse(localStorage.getItem('user')));            }            else                     if(mapnumber === 0)            {                mapnumber = 1.5;            }

There is a ton of code below for the map loading stuff.

 

Then im my main menu i have one button start the game without clearing the data and another starting a 'NEW' game and clearing the cache. The autosave removed and bound S key with timer to prevent spam of saving and done.

 

Hope this is helpful :)

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.