Fetching JSON on function call
prajwal.be.21

Hey,

So I may have a little trouble explaining this so please bare with me. I have a property bound to 'this' called tiles and when the game starts I load it up the following way. This tiles property is used to setup and mantain state of the board.

 

var self = this;

wade.loadScene('scene2.wsc', false, function () {
      wade.loadJson(tilesFile, data, function () {
          self.tiles = data.data.tiles;
          //More Code here
      });
});

And after fetching so, I use self.tiles to manipulate the board. So on the first gameplay it works fine.

Trouble arises when I move from one scene to another and also while reloading the same scene. So onclick of a button I load different scenes.
I do this by first clearning the scene then re-declare all values to origninal state like this.

 

this.startGame = function () {
    var data = {};
    var tilesFile = 'tiles.json';
    self.bluePawnsTiles = [1, 3, 5, 7, 8, 10, 12, 14, 17, 19, 21, 23];
    self.redPawnsTiles = [40, 42, 44, 46, 49, 51, 53, 55, 56, 58, 60, 62];
    self.drag = false;
    self.turn = 'red';
    self.direction = null;
    self.clickedTileObj = null;
    self.pawnNum = null;
    self.outOfBoundMove = false;
    self.invalidMove = false;
    wade.clearScene();

    wade.loadScene('scene2.wsc', false, function () {
      wade.loadJson(tilesFile, data, function () {
        self.tiles = data.data.tiles;
        //Run the game from here on 
        //More code
      });
    });
};

But the second time around, self.tiles still points to the old state, that is, the previous game state before loading new scene in the startGame function.

I set all properties to null and even set self.tiles to null. But I still have the same self.tiles as the previous session.

I tried wade.preloadJSON() as well but it is still no solution.

So basically my question is I want self.tiles to be reset to the original state as written in the tiles.json file and how it worked during the first gameplay.

Instead I end up with self.tiles still carrying the previous game state. Here is my tiles.json file.

 

{
    "tiles": [
        {
            "tileNo": 0,
            "x": -225,
            "y": -245,
            "occupiedBy": "none",
            "pawn": "none"
        },
        {
            "tileNo": 1,
            "x": -160,
            "y": -245,
            "occupiedBy": "blue",
            "pawn": "BluePawn0"
        },
        {
            "tileNo": 2,
            "x": -95,
            "y": -245,
            "occupiedBy": "none",
            "pawn": "none"
        },
        //More objects
        //More objects
        //More objects
        {
            "tileNo": 62,
            "x": 165,
            "y": 215,
            "occupiedBy": "red",
            "pawn": "RedPawn11"
        },
        {
            "tileNo": 63,
            "x": 225,
            "y": 215,
            "occupiedBy": "none",
            "pawn": "none"
        }
    ]
}

Any help would be really appreciated... Thanks!

All 2 Comments
Gio

Hi Prajwal

The problem here is that the first time you call wade.loadJSON, wade sends a request to your server to get the JSON data, then saves a copy of it and passes it to your callback.

You then somehow change this data, and later call wade.loadJSON again. This time, unless you tell wade to "fore reload" the data, wade will just give you the data it had saved the first time, without sending another request to the server. And this is the same data that you have modified. I hope this makes sense.

So in short, you could call wade.loadJSON with the 4th parameter (forceReload) set to true. This would send another request to the server to get the file again.

But I think it would be better if instead you did only one request to the server, creating a copy of the data before modifying it. The only thing you'd have to change in the code above is:

self.tiles = wade.cloneObject(data.data.tiles);

While I'm here I'll also take this opportunity to say that if you don't really need that data object to store the JSON, you can do the same thing in a simpler way, by using the argument that is passed to the callback directly:

wade.loadJson(tilesFile, null, function (data) {
    self.tiles = wade.cloneObject(data.tiles); // note this is NOT data.data.tiles
    //Run the game from here on 
    //More code
});

 

 

prajwal.be.21

Hey Gio,

Thanks for the help! It's working as expected now. I chose to implement the wade.cloneObject method.

Thanks a lot!

Post a reply
Add Attachment
Submit Reply
Login to Reply