Manipulating JSON data

0 favourites
  • 6 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • Hi!

    I've been learning about implementing JSON in Construct 3. I've read the manual, and done a little study on the wider web, but a few things still aren't clear to me.

    1) If I have an array in JSON which is, in turn, comprised of multiple key:attribute pairs, is there a way to use a given attribute to call for attributes of other keys within the same element of the array? For example, let's my JSON describes the parameters for a bunch of skills that the player can use. It looks something like:

    {

    "Skills": [ { "Name":"Punch", "baseDamage":1 }, { "Name":"Kick", "baseDamage":2 } ]

    }

    How do I tell Construct that when the player selects the skill named "Punch", it should use 1 in subsequent damage calculations, vs. 2 when the Skill is named "Kick"?

    2)What is the most efficient way to incorporate data from different JSON files into the same project? For example, skill data (names, damage, etc.) vs. enemy data (HP, attacks, etc.). I've seen an example where, I think, someone shifted AJAX.LastData to an array object as part of setup, then used AJAX to request the second JSON file, and parsed that with the JSON object. But I'm not sure whether that's the best way, or if I'm even understanding that process correctly.

    3) Related to Question 2, is there a way to make multiple JSON files talk to each other? So if data pulled from one JSON fulfills certain conditions, data in another file is altered?

    Thanks in advance for any help. Let me know if I can supply more information to aid in your response(s).

  • Usually you loop through a given path in json to find what you're looking for. Then when it matches, you use that path to look up the rest of the keys you're interested in at that path.

    The most efficient way to me is to organize it so that order doesn't matter. You can have all sorts of different json files, and they can all be loaded into memory/parsed at the start of the game, so that you have access to all the data given you know the path or key/attribute you're looking for. Just imagine if all the files were "included" into your project as one file.

    I normally don't have the game modify json files, only json that is loaded into memory. Considering all the files are loaded into memory and the file that they came from is not relevant, then they don't really need to talk to each other. If you need to write back to the json file, then write whatever is relevant.

  • 1. What you want will not work with an array, because you'll have to iterate it every time you need to get Punch or Kick values.

    I suggest you get rid of the array and change your JSON to this:

    {

    "Skills": { "Punch": {"baseDamage":1 }, "Kick": {"baseDamage":2 } }

    }

    This will allow you to access values by direct path like "Skills.Punch.baseDamage"

    2. I have lots of separate JSON objects in my project and load data into them at the beginning of the game with multiple AJAX requests.

    3. There are no built-in ways to sync data between different JSON objects, you will have to do this with code. For example, instead of directly changing damage value in JSON1, make a function "UpdateDamage", which will change it in both JSON1 and JSON2.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for your reply!

    I suggest you get rid of the array and change your JSON to this:

    {

    "Skills": { "Punch": {"baseDamage":1 }, "Kick": {"baseDamage":2 } }

    Does your recommendation hold if each skill has multiple keys associated with it? Base damage, energy cost, etc.? If so, would you mind explaining what the purpose of an array is, if each element can hold multiple key:attribute pairs of different types?

    2. I have lots of separate JSON objects in my project and load data into them at the beginning of the game with multiple AJAX requests.

    So if I parse Ajax.LastData, it remains parsed?

    3. There are no built-in ways to sync data between different JSON objects, you will have to do this with code. For example, instead of directly changing damage value in JSON1, make a function "UpdateDamage", which will change it in both JSON1 and JSON2.

    Thanks again for your responses. I hope my mismanagement of terminology is understandable, and not too cringy!

  • Thanks for your response!

    Usually you loop through a given path in json to find what you're looking for. Then when it matches, you use that path to look up the rest of the keys you're interested in at that path.

    What might that look like in terms of conditions and actions in construct?

    The most efficient way to me is to organize it so that order doesn't matter. You can have all sorts of different json files, and they can all be loaded into memory/parsed at the start of the game, so that you have access to all the data given you know the path or key/attribute you're looking for. Just imagine if all the files were "included" into your project as one file.

    I don't think the order matters. I've been operating from the assumption that once I parse a new AJAX.LastData, the previous AJAX.LastData is no longer accessible. But maybe that's not correct?

    I normally don't have the game modify json files, only json that is loaded into memory. Considering all the files are loaded into memory and the file that they came from is not relevant, then they don't really need to talk to each other. If you need to write back to the json file, then write whatever is relevant.

    Gotcha. Thanks again!

  • Does your recommendation hold if each skill has multiple keys associated with it? Base damage, energy cost, etc.?

    Of course. Each skill is still an object which can contain multiple keys or even nested objects and arrays.

    For example:

    {

    "Skills": { "Punch": {"baseDamage":1, "maxDamage":3, "cost": {"mana": 0, "energy": 5}}, "Kick": {....}

    Arrays in JSON may be useful when you need to store lots of similar values. For example an array of coordinates:

    "PatrolPath": [{"x":1, "y":1}, {"x":2, "y":2}, {"x":4, "y":2}, ....]

    So if I parse Ajax.LastData, it remains parsed?

    Yes, once you load data into a JSON object, it stays there of course. And later you can change it, add new keys, delete keys etc.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)