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

0 favourites
  • 5 posts
From the Asset Store
Progress\Loading Bar. Now Load you game like a Professional.
  • 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]]

    ]}

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

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

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

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