Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalStorage Instance

I'm developping a mobile game so i want to store the game progress for every logged in user ( if a player is in the mid game and accidentally closes the web browser. Once he opens the game again, the game starts from the beginning and the game that the player was playing is lost. In this way, players can resume their game later.)

So for every user i'll create a new localStorage object but i get this answer here Data supported by HTML5 Local Storage by "Jocelyne lecopmte" .

So it's not possible to do it ? if not how can'i do it so ? Thank you in advance

like image 481
Amira Manai Avatar asked Jul 02 '26 06:07

Amira Manai


1 Answers

That will work fine for any single player aspects of the game. You can use JSON.stringify and JSON.parse to save and load objects from localStorage.

var object = new Object();
object.score = 100;
object.name = "John";

localStorage.setItem("player1", JSON.stringify(object));
var copy = JSON.parse(localStorage.getItem("player1"));

console.log(copy.name);

Working code can be found here.

like image 121
Brant Olsen Avatar answered Jul 05 '26 16:07

Brant Olsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!