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

Get help using Construct 2

Post » Thu Oct 11, 2012 5:23 pm

How do I set/read a 2 dimensional array?

I always get:

[CODE]
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!
[/CODE]

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

Here is the code:



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.nutmix2012-10-11 17:25:11
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Thu Oct 11, 2012 6:26 pm

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.


nutmix2012-10-11 18:28:53
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Thu Oct 11, 2012 7:15 pm

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"
B
79
S
24
G
53
Posts: 4,728
Reputation: 40,219

Post » Mon Oct 15, 2012 4:27 pm

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.
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Mon Oct 15, 2012 4:33 pm

New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Mon Oct 15, 2012 4:35 pm

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

[CODE]
"{""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]]]}"
[/CODE]
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Mon Oct 15, 2012 5:56 pm

@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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,528

Post » Tue Oct 16, 2012 5:24 pm

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!nutmix2012-10-16 17:24:54
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Sat Jan 25, 2014 10:03 am

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.
B
5
S
1
G
1
Posts: 9
Reputation: 1,425

Post » Sat Jan 25, 2014 10:24 am

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)
[code]{
"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]]
]
}[/code]

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

However I found that my problem was this (as mentioned above)
[code]"size":[3,9,1],
[/code]

and now my calls work. very very frustrating.
B
5
S
1
G
1
Posts: 9
Reputation: 1,425

Next

Return to How do I....?

Who is online

Users browsing this forum: austinblackbelt and 33 guests