How do I Load an irregular 3d array from JSON?

Get help using Construct 2

Post » Sun Jun 18, 2017 1:56 pm

I have a json with irregular dimensions in a 3d array, but the load function fails to populate the array.
My json looks like this. What am I doing wrong?
{'c2array':true,'size':[2,30,30],'data': [[[], [20, 17]],
[[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
]}
B
5
S
1
Posts: 31
Reputation: 439

Post » Sun Jun 18, 2017 2:55 pm

It might be related to the fact that you're declaring the size of your array to be [2,30,30], but the actual size is [2,20,17]. I'm not sure how well it handles missing/empty values either.

Given that you're using the first X row to define the size of the rest of the data, couldn't you just make it a 2D array of [20,17,1]? You can always use the array.width and array.height values in conditions and actions, and add variables to the array to carry other data, rather than adding it into the array. You can also use the push and insert array actions to add extra values.
B
20
S
8
G
3
Posts: 458
Reputation: 4,055

Post » Sun Jun 18, 2017 3:21 pm

I also tried filling the full 2, 30, 30 with 0s on nonexistent values. I need this structure because the json contains 1 room, and in the future there will be multiple rooms with differing dimensions. I also tried setting the array values manually in code, and exporting that with asJson then loading it. It didn't work either.
B
5
S
1
Posts: 31
Reputation: 439

Post » Sun Jun 18, 2017 3:31 pm

Jesus, I figured it out. Unlike every other Json parser out there, the Construct 2 one cannot handle newlines. It must strictly be a 1 line string, and you have to load it using Ajax because the string parser forces you to change the inner quotations to single quotes, and it doesn't like that either.
B
5
S
1
Posts: 31
Reputation: 439

Post » Sun Jun 18, 2017 3:52 pm

I typically use this format structure for my 3D json arrays and import using AJAX:

{
"c2array":true,
"size":[2,3,4],
"data":[

[ [0,1,2,3], [0,1,2,3], [0,1,2,3] ],

[ [0,1,2,3], [0,1,2,3], [0,1,2,3] ]

]
}

C2 seems to be quite happy accepting the newlines (unless I'm misunderstanding you :) ).
B
20
S
8
G
3
Posts: 458
Reputation: 4,055


Return to How do I....?

Who is online

Users browsing this forum: Exabot [Bot], Kin, ratboy3, tinhofiel and 38 guests