Jump to content

Pushing json data into an inventory


jasmine
 Share

Recommended Posts

Hey everyone!
 

I am making a game with several items that each have multiple unique properties. I am storing the items for each level in separate json files for the sake of organization. Right now, I am trying to figure out the best method for putting the json data from each item acquired into an inventory system, something like the examples in this thread but using json data.  

 

Right now I am pushing the data into an array similar to the example above. It doesn't throw an error, but when I try to print out the inventory all I get is [object Object]. I know I need to do some kind of parse text function to ensure that Phaser is reading the item's data, but I'm not sure of the best way to go about that and I don't understand how they're doing it in the example. Does anyone have any ideas? 

 

Thanks! 

Link to comment
Share on other sites

I'm not sure what you mean about "print out the inventory". To the console? When you do a "console.log(inventory);" in your code it prints out "[object Object]" and doesn't let you expand anything?

 

If you're loading the JSON via Phaser, then the loader will parse the JSON for you and hand you back an object. Are you using "game.load.json" in a preload function somewhere?

Link to comment
Share on other sites

You should really show some code. This is how to print a json data

// itemsvar json = {    sword:     {name: "Steel Sword", attack: 5, defense: 10, gold: 10}    axe:       {name: "Steel Axe", attack: 12, defense: 2, gold: 5}};for (var key in json) { // scan through all json items    alert("Item Key [" + key + "]: name = " + json[key].name);    // Item Key [sword]: name = Steel Sword}
Link to comment
Share on other sites

You're right, I really should show some code. 

 

First of all, I'm making a shopping simulator. I have different json files with information about the products in each level. 

Here is an example of what those json files look like:

{  "background": "b1",  "levelButton":"start",  "nextL": "leveltwo",  "things": [    {      "asset": "red1",      "name": "",      "text": "",      "x": 65,      "y": 80,      "price":30,      "button": "buy",      "feel": ".2",      "picone": {        "one": "red1a",        "x": 250,        "y": 25      },       "pictwo": {        "two": "red1b",        "x": 300,        "y": 320      }    },    {      "asset": "red2",      "name": "",      "text": "",      "x": 65,      "y": 240,      "price": 25,      "feel": ".1",      "picone": {        "one": "red2a",        "x": 400,        "y": 320      },       "pictwo": {        "two": "red2b",        "x": 400,        "y": 320      }    },    {      "asset": "red3",      "name": "",      "text": "",      "x": 190,      "y": 80,      "price": 18,      "feel": ".04",      "picone": {        "one": "red3a",        "x": 400,        "y": 320      },       "pictwo": {        "two": "red3b",        "x": 400,        "y": 320      }    },    {      "asset": "red4",      "name":"",      "text": "",      "x": 190,      "y": 240,      "price": 12,      "feel": ".03",      "picone": {        "one": "red4a",        "x": 400,        "y": 320      },       "pictwo": {        "two": "red4b",        "x": 400,        "y": 320      }    }  ]}

I load the objects in my main state like this: 

//Getting player datainit: function(playerData) {    this.playerData = playerData ? playerData : {};    this.playerData.level = this.playerData.level ? this.playerData.level : 'levelone';  },//Loading level json file based off player data loadLevel: function(){    this.levelData = JSON.parse(this.game.cache.getText(this.playerData.level));     this.background = this.add.sprite(60, 60, this.levelData.background);     this.pictureOne = this.add.sprite (250, 25, '');     this.pictureTwo = this.add.sprite (290, 275, '');         //create things    this.things = this.add.group();    var thing;    this.levelData.things.forEach(function(thingData){      thing = new BuyApply.Purchased(this, thingData);      this.things.add(thing);    }, this);    return thing;    },

"Purchased" is a prefab that handles all the object behavior: 

var BuyApply = BuyApply || {};BuyApply.Purchased = function(state, data) {  Phaser.Sprite.call(this, state.game, data.x, data.y, data.asset);  this.game = state.game;  this.state = state;  this.anchor.setTo(0.5);  this.data = data;  this.price = data.price;  this.disc = data.disc;      //listen for the fucking input  this.inputEnabled = true;  this.input.pixelPerfectClick = true;  this.events.onInputDown.add(this.touch, this);};BuyApply.Purchased.prototype = Object.create(Phaser.Sprite.prototype);BuyApply.Purchased.prototype.constructor = BuyApply.Purchased;BuyApply.Purchased.prototype.touch = function() {this.state.inspoPic1.loadTexture(this.data.picone.one, 0);this.state.inspoPic2.loadTexture(this.data.pictwo.two, 0);this.state.panelLabel.text = this.data.text;this.state.statusText.text = this.data.name;this.state.priceText.text = '$' + this.data.price;this.description = this.state.add.sprite(240, 450, this.data.button);this.description.inputEnabled = true;this.description.events.onInputDown.add(this.buy, this);};BuyApply.Purchased.prototype.buy = function(){ //inventory is declared in the main class along with the other variables   //like this:  BuyApply.game.inventory= [];    this.game.inventory.push(this.data);    console.log(this.game.inventory);    console.log('this works');    this.game.desire += parseFloat(this.data.feel);     this.game.finalMoney = this.data.price + this.game.finalMoney;    console.log('final money ' + this.game.finalMoney);    if(this.game.money1 >= 0){        this.state.funds1.text = this.game.money1 -= this.data.price;        console.log('Money 1: ' + this.game.money1);     }    else if(this.game.money2 >= 0){        this.state.funds2.text = this.game.money2 -= this.data.price;        console.log('Money 2: ' + this.game.money2);    }    else if (this.game.money3 >=0){        this.state.funds3.text = this.game.money3 -= this.data.price;        console.log('Money 3: ' + this.game.money3);         }};

What I want to do now is push the json data of purchased items into a separate array (this.game.inventory) that will print out at the end of the game.  rn when i print out inventory it returns [object Object] which makes me think it's not parsing the data as a multi-fultifacted array or maybe you're right and I'm simply not printing it out correctly. I will try initializing the inventory var as an empty json and printing it out the way liakos suggested and report back

Link to comment
Share on other sites

1. as i see, "init() and loadLevel() are inside an "OBJECT" like this

var OBJECT = {   init: function() {   },   loadLevel: function() {      this.levelData = JSON.parse(...);   }};

Don't use this.levelData, but OBJECT.levelData. (I think "this" points to the loadLevel function and it's generally wrong in this case). change all "this." to "YOUR_OBJECT."

 

2.

this.levelData.things.forEach(function(thingData) {   });

"this.levelData.things" is the "things" array in your json file, right? "forEach" is a phaser method for phaser groups. in order to scan the array in json you must use

for (var i=0; i<this.levelData.things.length; i++) {   var thing = this.levelData.things[i];   // thing.price gets the price}

you have some advanced phaser code which i dont understand, but what you want is very simple

 

if you have an array

 

var array = ["alpha", "beta", "gamma"];

 

you scan it like

for (var i=0; i<array.length; i++) {   var value = array[i];}

its the same like having this object

var array = {   0: "alpha",   1: "beta",   2: "gamma"};

which you scan like

for (var key in array) {   var value = array[key];};

use "for (var key in array) {}" if you have array with holes or object

use "for (var i=0; i<array.length; i++) {}" if you have array with no holes

Link to comment
Share on other sites

In modern browsers, Array.prototype.forEach works to iterate over the members. In "tight" loops you probably don't want to make the iterating function over and over, but the syntax is fine.

 

Your code has this line: "this.levelData = JSON.parse(this.game.cache.getText(this.playerData.level));"

 

Why not "this.game.cache.getJSON"? How do you load the JSON data? As text, or as JSON?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...