[Solved]Load and save to JSON

Get help using Construct 2

Post » Mon Feb 24, 2014 9:04 am

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*
Last edited by QuincyDK on Thu Mar 06, 2014 10:50 am, edited 1 time in total.
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 9:48 am

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.
B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Mon Feb 24, 2014 10:23 am

Guizmus wrote: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.
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 10:31 am

@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)
B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Mon Feb 24, 2014 10:36 am

Guizmus wrote:@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();
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 12:15 pm

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.
B
7
S
2
Posts: 93
Reputation: 797

Post » Mon Feb 24, 2014 12:21 pm

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)
B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Mon Feb 24, 2014 1:09 pm

Guizmus wrote: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*
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 1:15 pm

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.
B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Mon Feb 24, 2014 2:03 pm

here is my text.capx file. The JSON has a data structure comparable (or actually directly ripped from) to our project.
B
7
S
2
G
2
Posts: 46
Reputation: 769

Next

Return to How do I....?

Who is online

Users browsing this forum: Felipiwi and 19 guests