Popuplate Select from JSON (Solved)

Get help using Construct 2

Post » Sat Aug 01, 2015 3:33 am

I have a select box that lets users pick a profile (save file) by name, given a list of existing saves. The game queries the server, which returns an array of names as JSON. I have confirmed that the JSON returned to the AJAX object is correct.

I have tried setting the array's contents from this JSON string, but the array is still full of 0's. Also, the array's size doesn't change when I do this!

Whether an array gets used or not, what's the best (or any working) way to fill in values of a select box from JSON?
Last edited by MoscowModder on Sat Aug 01, 2015 5:01 pm, edited 1 time in total.
B
9
S
1
Posts: 98
Reputation: 853

Post » Sat Aug 01, 2015 3:50 am

For a array to load properly from a .json it needs to be formatted in a particular way

{"c2array": true, "Size:"[2,3,0], "Data":[ [ ["Item Text 1"],["Item Text 2"],["Item Text 3"] ],[ ["Item Text 4"],["Item Text 5"],["Item Text 6"] ]]}

Load that with Ajax.LastData and you are on your way.

https://www.scirra.com/forum/why-can-c2-not-read-a-json-files-it-itself-makes-solved_t150889
I ask much the same question recently.
"Were all mad here. I'm mad, your mad." "How do you know I'm mad?" Asked Alice. "You must be" Said the Cheshire Cat. "Or you wouldn't have come here"-Lewis Carroll: Adventures In Wonderland
B
7
S
2
Posts: 113
Reputation: 1,245

Post » Sat Aug 01, 2015 4:27 pm

That seems so much like it should work... but I must still be doing something wrong because after the first element (of 3) gets added, I get a Javascript error about being unable to read property 0 of undefined.

My JSON looks like this (and it matches EXACTLY what I got when I hard-coded the values and output it using asJson):
Code: Select all
{
   "c2array" : true,
   "size" : [3, 1, 1],
   "data" : [[["Item1"], ["Item2"], ["Item3"]]]
}


I know this is stupid, but exactly how hard would it be to add a behavior for Selects that lets it import a 1D JSON array? I think this might be handy for other people as well. Keep in mind I'm somewhat experienced with JS but have never written a C2 plugin before.

P.S. In your example, one of the dimensions is 0. Shouldn't that make the array have a net size of 0?

EDIT: Ha! I got it to work by switching two dimensions in my size. From [3, 1, 1] to [1, 3, 1]. Thanks a ton.
B
9
S
1
Posts: 98
Reputation: 853

Post » Sat Aug 01, 2015 6:26 pm

MoscowModder wrote:That seems so much like it should work... but I must still be doing something wrong because after the first element (of 3) gets added, I get a Javascript error about being unable to read property 0 of undefined.

My JSON looks like this (and it matches EXACTLY what I got when I hard-coded the values and output it using asJson):
Code: Select all
{
   "c2array" : true,
   "size" : [3, 1, 1],
   "data" : [[["Item1"], ["Item2"], ["Item3"]]]
}


I know this is stupid, but exactly how hard would it be to add a behavior for Selects that lets it import a 1D JSON array? I think this might be handy for other people as well. Keep in mind I'm somewhat experienced with JS but have never written a C2 plugin before.

P.S. In your example, one of the dimensions is 0. Shouldn't that make the array have a net size of 0?

EDIT: Ha! I got it to work by switching two dimensions in my size. From [3, 1, 1] to [1, 3, 1]. Thanks a ton.


Your welcome, I should have mentioned that "size":[row,column,depth], sorry for the confusion.
"Were all mad here. I'm mad, your mad." "How do you know I'm mad?" Asked Alice. "You must be" Said the Cheshire Cat. "Or you wouldn't have come here"-Lewis Carroll: Adventures In Wonderland
B
7
S
2
Posts: 113
Reputation: 1,245


Return to How do I....?

Who is online

Users browsing this forum: evillair, gamecorpstudio, jorgmaquoi and 22 guests