[Solved]Load and save to JSON

Get help using Construct 2

Post » Mon Feb 24, 2014 2:06 pm

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.
Last edited by Guizmus on Mon Feb 24, 2014 2:08 pm, edited 1 time in total.
B
17
S
8
G
4
Posts: 461
Reputation: 6,147

Post » Mon Feb 24, 2014 2:08 pm

Ah, okay * goes to work *

Thank you :)
Last edited by QuincyDK on Mon Feb 24, 2014 2:08 pm, edited 1 time in total.
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 2:08 pm

Yeah sorry, I hit the Tab + space combo in quick reply box ^^ I edited it though
B
17
S
8
G
4
Posts: 461
Reputation: 6,147

Post » Mon Feb 24, 2014 2:11 pm

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

Post » Mon Feb 24, 2014 2:17 pm

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

Post » Mon Feb 24, 2014 2:19 pm

Well basically it's for an prototype of a media application. We store data (text, filename if relevant, filepath if relevant and info like that) into an json. That's the reason why we want to import json files into our project, to follow our data structure.

However, are multidimensional arrays within the scope of C2 Json?
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 2:21 pm

Arrays in C2 are all 3-dimensional. No more, no less. So it should be ok.
B
17
S
8
G
4
Posts: 461
Reputation: 6,147

Post » Mon Feb 24, 2014 2:42 pm

Guizmus wrote:Arrays in C2 are all 3-dimensional. No more, no less. So it should be ok.


I'm terribly sorry to bother you again..

Now I have this JSON

Code: Select all
{"c2array":true,"size":[10,1,1],"data":[[["Test1"]],[["Test2"]],[["3"]],[[40]],[[5.0]],[[60000]],[[7,0]],[[8.000.000]],[[9*10]],[[10]]]}


I load this into my project with AJAX and (try to ) parse this into my array. When I call Array.At(0) I get an 0 back. What would be the correct way to call from the array say the 2nd position? (Array.At(1) I guess right?)

Error is fixed. Anyhow, how can I populate instance variables with JSON data at index 0, 1 etc etc? That's more or less the question.


This is all run from preview btw.

Edit: Also from my own server (using an export) I get nothing.
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 3:51 pm

To be more percise: I call the AJAX command to pull in the JSON. However, when I set "Text to AJAX.LastData" , I get an empty text container. This makes me think that there is something in the data retrieval that doesn't work.
B
7
S
2
G
2
Posts: 46
Reputation: 769

Post » Mon Feb 24, 2014 4:16 pm

Let's go for another example but first some things :
- use the "Debug layout" feature. This way, you can check in the console the content of an array without having to display it. Same goes the AJAX.LastData
- in C2, an error we all do at first : if a text object isn't big enough to write its text, it will cut after the last word it can display. In your case, 1 word only (no spaces) too big => no display. It works perfectly.
- your json has some values C2 doesn't accept, like 8.000.000. It can't load your array from JSON and doesn't raise any errors.

So I did an example, where, check the events :
- events 1-3 : I load an array and do some debug on it to display every data it holds. (this part isn't active)
- event 4-5 : I populate manually an array with the datas you wanted, and download it to check the format of the JSON

So to sum up :
- don't display big ass texts in little objects
- don't use unsuported types in array (stick with int, float and string)
B
17
S
8
G
4
Posts: 461
Reputation: 6,147

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: dahu, ketramtu18, Syazanie Amirin and 8 guests