Trying to Load JSON String

0 favourites
  • 7 posts
  • 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:

    {
         "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] ] ]
    }
    

    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!

  • 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.

  • 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

    {
         "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] ] ]
              }
    }
    

    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.

  • 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!

    {
         "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] ] ]
    }
    
  • I think I got it!

    Thanks. :-)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Can you share how you got it to work? That would be much appreciated.

  • I'm using rexrainbow's hash table plugin. I'll post more details later!

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