Reputation: 291
I've created a game based on HTML5+JS using melonJS for Windows8.
But, how do I save player's persisting game data into the disk file (preferably localFolder).
I've read the sample given in the MSDN forum, but there's no mention about saving it to JSON format file... and plus I'm a little new to Win8 app programming.
Anyways, this is what I tried (this function is called when player choose to save):
function meSave() {
//create a var to store the the persisting data during the play
var dataSaved = {
data: {
progress: game.data.progress,
HP: game.data.HP,
MP: game.data.MP,
Level: game.data.Level,
maxHP: game.data.maxHP,
maxMP: game.data.maxMP,
Money: game.data.Money,
currPos: {
x: me.game.getEntityByName('mainPlayer')[0].pos.x,
y: me.game.getEntityByName('mainPlayer')[0].pos.y,
},
currStage: me.levelDirector.getCurrentLevelId(),
}
};
var applicationData = Windows.Storage.ApplicationData.current;
var localFolder = applicationData.localFolder;
var filename = "dataFile.json";
function writeTimestamp() {
localFolder.createFileAsync("dataFile.json",
Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function (file) {
return Windows.Storage.FileIO.writeTextAsync(file,
JSON.stringify(dataSaved));
}).done(function () {
game.savingDone = true;
});
}
}
Upvotes: 1
Views: 2011
Reputation: 59763
The most obvious issue seems to be that you're not calling the function writeTimestamp
in your code. There are also a handful of other things that you might consider doing:
meSave()
.done(function () {
console.log('saved!');
});
function meSave() {
//create a var to store the the persisting data during the play
var dataSaved = {
/* put or fetch your game data here */
};
var applicationData = Windows.Storage.ApplicationData.current;
var localFolder = applicationData.localFolder;
var filename = "dataFile.json";
return localFolder.createFileAsync(filename,
Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function (file) {
return Windows.Storage.FileIO.writeTextAsync(file,
JSON.stringify(dataSaved));
}).then(function() {
// dataSaved.done ... although this would be lost if dataSaved is local
});
}
I changed a few things:
meSave
function. I've replaced the function with another Promise
. In this way, your calling code can rely on the callbacks
of the Promise
to be aware that the file has been saved to storage successfully. done
for the file creation was setting a variable that didn't exist locally (maybe it's somewhere else in your code). Upvotes: 1