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

0 favourites
  • 10 posts
From the Asset Store
7 Errors
$15 USD
7 Errors is a game where you have to find the 7 mistakes before time runs out. Can you find all 7? Have fun looking.
  • How do I set/read a 2 dimensional array?

    I always get:

    Javascript error!
    TypeError: this.arr[x][y] is undefined
    http://172.30.5.17:50000/Arr_plugin.js, 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

    etc.

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

    [[

    [0],[0],[0]],[[1],[11],[0]],[[2],[12],[0]...

    I was expecting:

    [[

    [0],[1],[2],[3],[4]],[[[00],[01]...

    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.

  • Ok, got it working, using this (which has the X&Y order reversed):

    "{""c2array"":true,""size"":[5,68,1],""data"":[[[0],[1],[2],[0],[1],[2],[0],[1],[2],[11],[4],[5],[6],[7],[0],[1],[2],[0],[1],[2],[0],[10],[2],[3],[4],[5],[6],[2],[0],[1],[2],[3],[0],[1],[2],[0],[1],[2],[0],[1],[2],[0],[1],[2],[4],[5],[0],[1],[2],[0],[1],[2],[0],[1],[3],[0],[1],[4],[3],[2],[11],[1],[10],[3]],[[0],[1],[2],[0],[1],[2],[0],[1],[1],[11],[4],[5],[6],[7],[0],[1],[2],[0],[1],[2],[0],[10],[2],[3],[4],[5],[6],[2],[0],[1],[2],[3],[0],[1],[2],[0],[1],[2],[0],[1],[2],[0],[1],[2],[4],[5],[0],[1],[2],[0],[1],[2],[0],[1],[3],[0],[1],[4],[3],[2],[11],[1],[10],[3]],[[0],[1],[2],[0],[1],[2],[0],[1],[1],[11],[4],[5],[6],[7],[0],[1],[2],[0],[1],[2],[0],[10],[2],[3],[4],[5],[6],[2],[0],[1],[2],[3],[0],[1],[2],[0],[1],[2],[0],[1],[2],[0],[1],[2],[4],[5],[0],[1],[2],[0],[1],[2],[0],[1],[3],[0],[1],[4],[3],[2],[11],[1],[10],[3]],[[0],[1],[2],[0],[1],[2],[0],[1],[1],[11],[4],[5],[6],[7],[0],[1],[2],[0],[1],[2],[0],[10],[2],[3],[4],[5],[6],[2],[0],[1],[2],[3],[0],[1],[2],[0],[1],[2],[0],[1],[2],[0],[1],[2],[4],[5],[0],[1],[2],[0],[1],[2],[0],[1],[3],[0],[1],[4],[3],[2],[11],[1],[10],[3]],[[0],[1],[2],[0],[1],[2],[0],[1],[1],[11],[4],[5],[6],[7],[0],[1],[2],[0],[1],[2],[0],[10],[2],[3],[4],[5],[6],[2],[0],[1],[2],[3],[0],[1],[2],[0],[1],[2],[0],[1],[2],[0],[1],[2],[4],[5],[0],[1],[2],[0],[1],[2],[0],[1],[3],[0],[1],[4],[3],[2],[11],[1],[10],[3]]]}" 
    
  • 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?

    Thanks!

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

  • Try Construct 3

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

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

    {
    "c2array":true,
    "size":[9,3,1],
    "data":[
    [[3,1,3,0.5,1,60,"n",0,0]],
    [[5,2,10,0.2,3,340,"y",100,200]],
    [[15,3,5,1,2,180,"n",0,0]]
    ]
    }

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

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

    "size":[3,9,1],
    

    and now my calls work. very very frustrating.

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