Jump to content

Save game data locally


Biggerplay
 Share

Recommended Posts

I haven't used Phaser yet so I don't know whether there is any abstraction provided there, but when coding localStorage handling for my own games I found this article very helpful:

 

http://diveintohtml5.info/storage.html

 

There's also a smashing-magazine one that looks like it covers much of the same ground

 

http://coding.smashingmagazine.com/2010/10/11/local-storage-and-how-to-use-it/

Link to comment
Share on other sites

Idk why you would need any library to use localstorage... Oo

 

Its really just:

localStorage.setItem('myItemKey', 'myContent');

and

localStorage.getItem('myItemKey');

Only thing to keep in mind: localstorage can only store strings.
So in order to store whole objects, do this:

localStorage.setItem('myObject', JSON.stringify(myObject));

and in reverse:

myObject = JSON.parse(localStorage.getItem('myObject'));
Link to comment
Share on other sites

 

Idk why you would need any library to use localstorage... Oo

 

Its really just:

localStorage.setItem('myItemKey', 'myContent');

and

localStorage.getItem('myItemKey');

Only thing to keep in mind: localstorage can only store strings.

So in order to store whole objects, do this:

localStorage.setItem('myObject', JSON.stringify(myObject));

and in reverse:

myObject = JSON.parse(localStorage.getItem('myObject'));

 

Mainly backwards compatibility - something like Amplify allows you to store using a single API which gracefully degrades from localStorage to sessionStorage, globalStorage, userData and finally an in-memory store if none of the above is available. This ensures compatibility with browsers as far back as IE5... Not that your game would otherwise work in that browser ;)

Link to comment
Share on other sites

On a side note , if you do use local storage to store level data and user data, your game will be easily hackable by any user from the browser console (unless you do some crazy encrypt code and checking) .  I always use local storage becuase I don't care if my users hack the game to get to more advanced levels but this might be something you might not want to happen.  Yeah and there's no need to use a library to use local storage for game. plus if you're new to javascript and html5 then i would recommend learning how js works before using a library. especially for something as simple as local storage.

Link to comment
Share on other sites

  • 3 months later...
  • 2 weeks later...

@Vaughan

  Sometimes its just a case of not making it too easy.  Once it becomes general knowledge that games store things in local storage and all the kid needs to do is go to the web inspector and have a look, it all gets too easy.  So even if its really weak as hell at least its going to take looking in the code to cheat and not just local storage.

For instance, which one of these is easier to cheat with?.....

{lives:3}

....or...

ÅÌ

And thats using one of the oldest, weakest methods there is ;)......

function dyslexia(string){return String.fromCharCode.apply(this,string.split('').map(function(a){return a.charCodeAt()^255;}))}

var a = "{lives:3}";

var z = dyslexia(a);

a == dyslexia(z);

 

PS.  I dont think that function would work right with unicode stuff (couldnt be bothered checking), if thats the case just btoa it first and in your ascii land.

Link to comment
Share on other sites

For me, if its a single player game that runs entirely locally, no backend, and doesn't share scores socially or integrate externally, I wouldn't even bother with any attempt to protect the local storage data. If they want to cheat fine, at least they are still playing your game! :)

Link to comment
Share on other sites

@Rudy  But for much less time than they would if they didnt cheat.  Plus from a couple of people I know....If they can cheat their appreciation of the game will drop.  I thnk this because of my bro in law.  He use to cheat on EVERYTHING!  I use to spend alot of time making cheats for him on the original xbox.  Then 360 came out and he couldnt cheat anymore....Hes the one that told me that since not being able to cheat he actually enjoyed playing games more.  Why?  Who knows exactly.  But personally I think its because hes pushed through the frustration of loosing and learnt that joy full feeling of being a true winner.  Its the pain followed by the completion that truly makes you feel.

Link to comment
Share on other sites

  • 6 years later...
On 1/30/2014 at 7:46 AM, Chris said:

i did it but i dont think it works? pls help

 

Idk why you would need any library to use localstorage... Oo

 

Its really just:


localStorage.setItem('myItemKey', 'myContent');

and


localStorage.getItem('myItemKey');

Only thing to keep in mind: localstorage can only store strings.
So in order to store whole objects, do this:


localStorage.setItem('myObject', JSON.stringify(myObject));

and in reverse:


myObject = JSON.parse(localStorage.getItem('myObject'));

 

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...