2d array + json + at(x,y) = javascript error

0 favourites
  • 10 posts
From the Asset Store
Advanced inventory mechanics for your RPG game (Array-based). Take Items, split them, pick up them, read the description
  • How do I set/read a 2 dimensional array?

    I always get:

    Javascript error!
    TypeError: this.arr[x][y] is undefined, line 96 (col undefined)
    This is either a bug in Construct 2 or a problem in a third party plugin or behavior - please report it to the developer!

    I'm trying to set a 2 dimensional array, then read a value from it.

    Here is the code:

    <img src="http://ohds.co.uk/c2/array-json.PNG" border="0" />

    I was expecting it to output 3, but get a JS error.

    If I dump the array as json, I get the same json as I put in, so presumably this means I used the correct (undocumented?) format.

    I also tried reels.At(2,2,0), but this generates the same error.

  • Not found a solution yet, but the json array structure seems to have X and Y the wrong way round.

    set reels to be 5x3x1 (i.e. x=0-4, y=0-2)

    I tried

    set value at (0,0) to 0

    set value at (1,0) to 1

    set value at (2,0) to 2

    set value at (3,0) to 3

    set value at (4,0) to 4

    set value at (0,1) to 00

    set value at (1,1) to 11

    set value at (1,2) to 12


    But when I display "reels.AsJSON" I get some counter intuitive results something like:



    I was expecting:



    It would appear that the Y comes first, as if the array was 3x5 not 5x3.

  • I think it's probably easier to just make another utility capx to make the arrays and output the the ASJSON's. As it is now it's only purpose is to give a text representation of the array. It correctly mirrors how C2 stores the array internally, and I think it is unlikely that it will get changed as it would break any capx that uses "load from JSON"

  • I still cant figure out away to get a 2d array into an array object using the load from json string. Has anyone done this, and perhaps can supply an example? I cant really do it using "set value" as I have 300 data points.

    Any help much appreciated.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok, got it working, using this (which has the X&Y order reversed):

  • R0J0hound's suggestion is a good idea, you really shouldn't be trying to edit this data by hand - just only ever use anything saved directly out of the Array object and it should always work as expected.

  • To statically initialize a big constant array with several hundred values, is there a way other than writing your own json object (as I have done), or hundreds of set statements?


  • I am getting the exact same problem in the exact same situation. I don't understand how I can load a JSON file into an array and then dump that array to JSON AND at the same time not be able to access anything beyond the very first Y of each X.

    It's an array, we should be able to access any X / Y / Z without JS complaining.

  • And as per usual I discover that it's a PEBCAK issue. It was a disconnect between X and Y on loading the JSON and on the call.

    Below is my original JSON string (of course placed inline)


    Now calling at(0,0) works fine but at(0,5) doesn't.

    However I found that my problem was this (as mentioned above)


    and now my calls work. very very frustrating.

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