[Solved]Load and save to JSON

0 favourites
From the Asset Store
Match the similar cards with each other and free all animals!
  • Hi there,

    We currently use an array for datastorage, but to follow our back-end development we'd like to use JSON files instead.

    The documentation of CS2 isn't getting me anywhere, so could anyone explain me how this all works? *is new to CS2*

  • For runtime, using the array will be easier.

    For saving/restoring purposes you can get the datas of an array object as JSON quite simply, using the expression "Array.AsJSON". Then, you have to use this JSON. As it's for a back-end dev, I would suggest sending it via AJAX.

    Something like this.

  • For runtime, using the array will be easier.

    For saving/restoring purposes you can get the datas of an array object as JSON quite simply, using the expression "Array.AsJSON". Then, you have to use this JSON. As it's for a back-end dev, I would suggest sending it via AJAX.

    Something like this.

    I can't deny that this is very interesting and a good tip, the reading from json is more important right now as the json will be created backend. However, this is merely for a prototype thus everything will be "static". For this purpose, we'd like to read from a json file (multidimensional would be prefered) and use this in our CS2 prototype.

  • QuincyDK

    i'm not sure I follow entirely what you want but here is my take on it.

    If you need to populate an array based on a JSON you build outside of C2, there are 2 ways :

    • if you want to load the JSON during runtime, you could have the JSON file in the project files, and just replace it on your server. Use an event "On start of layout : AJAX load project file myJSON.json" and another "AJAX on finished loaded : Array.loadFromJSON(AJAX.LastData)"
    • if you want to populate the array before the runtime (so inside the C2 editor), just add an event "on start of layout : Array.loadFromJSON(X)", where X is the string representing the JSON content (in one line of course).

    If you need to have the format of an Array JSON representation in C2, just do some Alert or console.log of a populated array during any runtime, and copy paste its "asJson" expression.

    I hope this was what you were looking for, I'm not sure at all x)

  • QuincyDK

    i'm not sure I follow entirely what you want but here is my take on it.

    If you need to populate an array based on a JSON you build outside of C2, there are 2 ways :

    - if you want to load the JSON during runtime, you could have the JSON file in the project files, and just replace it on your server. Use an event "On start of layout : AJAX load project file myJSON.json" and another "AJAX on finished loaded : Array.loadFromJSON(AJAX.LastData)"

    - if you want to populate the array before the runtime (so inside the C2 editor), just add an event "on start of layout : Array.loadFromJSON(X)", where X is the string representing the JSON content (in one line of course).

    If you need to have the format of an Array JSON representation in C2, just do some Alert or console.log of a populated array during any runtime, and copy paste its "asJson" expression.

    I hope this was what you were looking for, I'm not sure at all x)

    I'm terribly sorry to keep on asking, but I'm new to C2. Where would I find this AJAX object? Is AJAX a plugin or..? Also: Where could I insert raw JS (Alert(); and console.log();

  • The AJAX object is inserted to the project the same way as any other object such as a Sprite, Tiled Background, Sprite Font, Array etc. It's at the bottom you may have missed it.

    You'll also find one in the same place called Browser, the Browser object will let you execute javascript directly.

  • And here is an example on how to download an array as a json file, and how to load an array from a json project file. (the project file is empty, it's only for the events)

  • And here is an example on how to download an array as a json file, and how to load an array from a json project file. (the project file is empty, it's only for the events)

    Thanks alot! I think the reading goes correctly now Problem number 2, parsing it.

    RIght now I have (built alongside your example:

    EVENT SHEET {

    1. SYSTEM | On start of layout [ AJAX | Request testing.json (tag JSON)]

    2. AJAX | On "JSON" completed [ Array | Load from JSON string AJAX.LastData ]

    3. MOUSE | On Left button Clicked on [T] Text [Text2 | Set test tp Array.At(0) ]

    I guess you can see what I try to do here. If user clicks on text2, I want the text to change to the first key in the array. Ofcourse, since I'm such a noob at C2, this doesn't work :') *ashamed*

  • Just a question then : what is inside your "testing.json" file ? Like I said, in my example, the json file was empty.

    My first guess here (you should post your capx, it's easier to find, not to guess) is that your json is empty, thus your array is empty, thus the text stays empty.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • here is my text.capx file. The JSON has a data structure comparable (or actually directly ripped from) to our project.

  • The problem is your json file. It has its own structure, but if you want to directly load the JSON inside the array, you have to have the same structure as the array in C2 has. To know how to represent an array in JSON in C2, you start by downloading the array (like I put in my example before).

    By the way, you won't be able to have associative arrays in C2. Declare some constants (some defines) to access more easily the keys if you want to, but the JSON provided won't be usable as it is.

  • Ah, okay * goes to work *

    Thank you

  • Yeah sorry, I hit the Tab + space combo in quick reply box ^^ I edited it though

  • So basically I'm stuck with an 0-index-based aray? If so, could I just create variables and populate them by using the 0-based index of the aray?

  • Yes, you have only 0-based arrays. If you want to access the first line, representing the "KNIGHT" characteristics for example, you could create variables :

    • "KNIGHT" = 0
    • "BASE_LIFE" = 0
    • "BASE_ATTACK" = 1

    And access the array with Array.At(KNIGHT,BASE_LIFE), or Array.At(KNIGHT,BASE_ATTACK) instead of Array.At(0,0).

    If I'm guessing right and you are aiming to store pre-computed characteristics of object types with the array, I would suggest you try the unofficial CSV plugin, as it seems quite adequate here.

    EDIT : and by variables I mean constants. I would put them in a separated event sheet named "define" and include this sheet everywhere. You seem to be quite familiar with algorithm and programming language so this should go without explanation.

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