How does a one-page game webapp remember a game state + high scores

Viewed 24

I've been playing 2048 in my browser and I want to make a game web app of a similar nature. I've noticed that the site remembers your best score as well as the last state of the game you were last playing. I want the same to happen in my own game web app.

Any idea how 2048 does this? Is it the IP address? If I do that am I legally obliged to let the user know?

2 Answers

You have two options.

  1. Use localStorage, as it persists on a device
  2. Use a backend server, that links to a database like MongoDB, SQL, etc. that is then able to store high scores based on user accounts/info.

For a game that is not highly complex, its better to use localStorage, since it stores data on a client's machine in the form of strings.

As a result, you don't NEED to let the user know about this, because all the data is hosted locally, and it is not sensitive info, like passwords, usernames, credit cards, names, etc. However, putting in a statement about how their data is used, and is stored locally, is never a bad thing, and its better to be safe than sorry.

2048 uses localstorage because their game doesn't compare your score to others or is an online multiplayer game.Therefore, you don't have to worry about hosting other servers, security, etc.

However, if you ever want to display a leaderboard or anything that requires a mix of various user scores, think like fortnite leaderboards, or any online multiplayer game, you will require a backend server linked to a database. This is highly complex though, and requires knowledge related to backend, database, and networking infrastructure and overall system design.

It's pretty simple. Each time the player sets a new high score, you can save the score in localStorage. Then you can look for the key that represents the score in localStorage each time the game reloads.

More about localStorage

Related