Trying to Load JSON String

Get help using Construct 2

Post » Fri Jul 27, 2012 4:24 am

I'm trying to load a JSON string (from a project file) that defines the layout of various levels for my game. Here's what the level_definition.json file contains:

[code]
{
     "Simple Tap": [ [ [1] ] ],
     "Double Tap": [ [ [1],[2] ] ],
     "Triple Tap": [ [ [1],[2],[3] ] ],
     "Quad Tap": [ [ [1],[2],[3],[4] ] ],
     "Quad Tap Double Back": [ [ [1],[2],[4],[3] ] ],
     "Single Double": [ [ [1,3],[2] ] ],
     "Triple Back and Forth": [ [ [1],[2,4],[3] ] ],
     "Three Drum Intervals": [ [ [1,3],[2],[4] ] ],
     "Angled Triple": [ [ [1],[2] ], [ [3],[0] ] ],
     "Four Drum Square Reverse": [ [ [1],[2] ], [ [3],[4] ] ],
     "Four Drum Square Rounder": [ [ [2],[1] ], [ [3],[4] ] ]
}
[/code]

So, each object has a name and a layout (as an array).

When I read it in, I get this result (appending array_Levels.AsJSON to the log of a DebugPanel window): {"c2array":true,"size":[1,1,1],"data":[[[0]]]}

1) Is my format wrong?!
2) If it's the correct format, can the AJAX object not import it for some reason? My guess is, it chokes on the multi-level array definition. If that's the case, how can I otherwise read this into a C2 array?

Thank you!
B
21
S
5
Posts: 43
Reputation: 2,529

Post » Fri Jul 27, 2012 2:09 pm

Is that supposed to be one large array or a list of smaller ones? Are you loading the whole thing into an array as a JSON string?
The format should be like this one:
{"c2array":true,"size":[1,1,1],"data":[[[0]]]}
You need the "c2array", "size", "data" etc.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,000

Post » Fri Jul 27, 2012 3:09 pm

Yes, it's one large array. I'm trying to put it all into an array. (Maybe I should use a dictionary, but I didn't know if dictionaries could have arrays as their value content.)

Maybe that's why I'm getting that message. It's telling me my JSON is not formatted properly! Thanks, ramones. I think I understand now.

What I should use is

[code]
{
     "c2array":true,
     "size":[11,1,1],
     "data":
          {
               "Simple Tap": [ [ [1] ] ],
               "Double Tap": [ [ [1],[2] ] ],
               "Triple Tap": [ [ [1],[2],[3] ] ],
               "Quad Tap": [ [ [1],[2],[3],[4] ] ],
               "Quad Tap Double Back": [ [ [1],[2],[4],[3] ] ],
               "Single Double": [ [ [1,3],[2] ] ],
               "Triple Back and Forth": [ [ [1],[2,4],[3] ] ],
               "Three Drum Intervals": [ [ [1,3],[2],[4] ] ],
               "Angled Triple": [ [ [1],[2] ], [ [3],[0] ] ],
               "Four Drum Square Reverse": [ [ [1],[2] ], [ [3],[4] ] ],
               "Four Drum Square Rounder": [ [ [2],[1] ], [ [3],[4] ] ]
          }
}
[/code]

Oddly, neither the "Array" entry nor the "AJAX" entry indicate that the format of the JSON string being loaded need be particular. It would have saved us time if the docs had said something like, "To load a JSON string, it must be in this format: { ... }" Maybe it does and I missed it.
B
21
S
5
Posts: 43
Reputation: 2,529

Post » Fri Jul 27, 2012 6:08 pm

I've tried using the hash plugin as well, but can't get it to successfully load up a JSON file into an array for me. There might be a better way to approach this, but I'm new to C2 and don't yet know all the intricacies.

Each key below is the name of a level of my game. Each value represents a grid (or array), from 1x1 for "Simple Tap," to 2x2 for "Four Drum Rounder."

Ultimately, I'd like to call up a level and plot on the screen the pattern. How do I load this into C2 so I can make use of it in that way?

Thank you!

[code]
{
     "Simple Tap": [ [ [1] ] ],
     "Double Tap": [ [ [1],[2] ] ],
     "Triple Tap": [ [ [1],[2],[3] ] ],
     "Quad Tap": [ [ [1],[2],[3],[4] ] ],
     "Quad Tap Double Back": [ [ [1],[2],[4],[3] ] ],
     "Single Double": [ [ [1,3],[2] ] ],
     "Triple Back and Forth": [ [ [1],[2,4],[3] ] ],
     "Three Drum Intervals": [ [ [1,3],[2],[4] ] ],
     "Angled Triple": [ [ [1],[2] ], [ [3],[0] ] ],
     "Four Square Reverse": [ [ [1],[2] ], [ [3],[4] ] ],
     "Four Square Rounder": [ [ [2],[1] ], [ [3],[4] ] ]
}
[/code]
B
21
S
5
Posts: 43
Reputation: 2,529

Post » Fri Jul 27, 2012 11:35 pm

I think I got it!

Thanks. :-)
B
21
S
5
Posts: 43
Reputation: 2,529

Post » Sun Jul 29, 2012 4:31 pm

Can you share how you got it to work? That would be much appreciated.
B
19
S
6
G
1
Posts: 116
Reputation: 2,730

Post » Sun Jul 29, 2012 7:49 pm

I'm using rexrainbow's hash table plugin. I'll post more details later!
B
21
S
5
Posts: 43
Reputation: 2,529


Return to How do I....?

Who is online

Users browsing this forum: austinblackbelt, Matthew de, Skebab and 6 guests