I'm trying to create a save/load function for my game in js, but I have basically no idea with how to go through with doing this. I can save variables to a JSON file or LocalStorage, but I don't know how to load them back into the program. I'm also pretty sure I'm exporting variables the wrong way as well. Any help?
Asked
Active
Viewed 254 times
-1
-
5Some code would be helpful. – Pärt Johanson Mar 03 '19 at 16:36
-
Right now this question is pretty broad. Can you start by googling something like "JavaScript load from local storage"? What results do you see? – Kevin Workman Mar 03 '19 at 16:47
-
Possible duplicate of [Storing Objects in HTML5 localStorage](https://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage) – Nino Filiu Mar 03 '19 at 18:48
-
Take a look at the documentation here https://github.com/processing/p5.js/wiki/Loading-external-files:-AJAX,-XML,-JSON It should at least allow you to ask a more specific question. – Charlie Wallace Mar 04 '19 at 17:17
2 Answers
1
Normally, I use JSON format to store and read data (of any type).
To save data (using key gamedata
as example):
var myData = {
name: 'David',
score: 10
}
localStorage.setItem('gamedata', JSON.stringify(myData));
** without JSON.stringify, you data will be saved as string [Object object]
To retrieve the data back:
var savedData = localStorage.getItem('gamedata'); // savedData is string
var myData = JSON.parse(savedData); // parse JSON string to java object

Ponleu
- 1,492
- 12
- 27
0
setup a bin on www.myJSON.com. p5 has built in functionality for ajax requests such as loadJSON. that way it's not in local storage and you can access your data if you have it on github. I know your struggle, I used to deal with this sort of issue myself before I found myJSON

Matt-the-Marxist
- 164
- 1
- 1
- 12