How do I save the data from an array to an existing JSON?

Get help using Construct 2

Post » Mon Jul 13, 2015 6:52 pm

Hello

There are 5 arrays in my game, each meant to provide data for one of 5 campaigns and after the game starts they each load that data from one of 5 JSON project files. During the game the data in the arrays changes, like number of completed levels or level scores and such and ideally I would like the game to save the data in the arrays back to their respective JSON files but I have no idea how to do it.

I've been searching the forum for an hour and since I can't find anything I will assume there's a simple catch I'm missing (again). I've managed to save the modified data from my arrays using Local Storage and then load it back up but Local Storage creates it's own files apparently and that leaves me with 5 extra JSON files that are only used on the very first run of the game. Right now it's not a big deal but it will be once I start using arrays and JSON to save levels.

So is there a way to simply save the data from an array back to the JSON project file it was loaded from?
B
7
S
1
Posts: 29
Reputation: 483

Post » Mon Jul 13, 2015 8:33 pm

Probably not. For security reasons Javascript tends to limit access to various things, otherwise the web would be full of dodgy scripts which changed all sorts of things. Javascript is very sandboxed.

I don't quite see why you'd want to. Your 5 JSON files are your games initial state, and your localstorage version is the current state of the game. Overwriting your original files sounds like a bad idea - supposing you wanted to restart the whole game, where would you get your original data from.

More to the point, does it *matter* ? JSON files, even quite complicated ones, take up very little space compared to sound or graphics files.
My tutorial through development blog is at http://myconstruct2dev.blogspot.co.uk/
B
12
S
3
Posts: 221
Reputation: 1,328

Post » Mon Jul 13, 2015 8:39 pm

paulscottrobson wrote:Probably not. For security reasons Javascript tends to limit access to various things, otherwise the web would be full of dodgy scripts which changed all sorts of things. Javascript is very sandboxed.

I don't quite see why you'd want to. Your 5 JSON files are your games initial state, and your localstorage version is the current state of the game. Overwriting your original files sounds like a bad idea - supposing you wanted to restart the whole game, where would you get your original data from.

More to the point, does it *matter* ? JSON files, even quite complicated ones, take up very little space compared to sound or graphics files.


Well, at least I wasn't overlooking something. Thanks for explaining, I hadn't thought about the initial state issue so I guess I'll just continue with LocalStorage + starting JSON files.
B
7
S
1
Posts: 29
Reputation: 483


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 26 guests