1

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;
        });
    }   
}
Dan
  • 9,391
  • 5
  • 41
  • 73
ayakashi-ya
  • 261
  • 3
  • 18

1 Answers1

1

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:

  1. There wasn't a need for an inner named function inside of the 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.
  2. You had extra variables (unused), and the done for the file creation was setting a variable that didn't exist locally (maybe it's somewhere else in your code).
WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • 1
    Why aren't you just returning the promise from createFileAsync and the rest of the chain, and fulfilling it with the savingDone flag? Just change the last .done to a .then. That way you don't need the new WinJS.Promise that you aren't fulfilling with any meaningful value, and eliminate the dataSaved.savingDone which goes out of scope after all this completes anyway. – Kraig Brockschmidt - MSFT Feb 01 '14 at 16:32
  • thank you for the answer. i call the `meSave` from a button click and the `savingDone` flag was for the save button to show a dialog box when it finish saving. @Kraig: considering your suggestion, then it would be `return localFolder.createFileAsync(.....).then(//writeJSON).then( function() {game.savingDone = true});` ? – ayakashi-ya Feb 04 '14 at 13:34
  • @KraigBrockschmidt-MSFT - apparently I wanted to have a party of Promises. I've uninvited some of the guests based on your excellent suggestion. :) – WiredPrairie Feb 04 '14 at 14:17