Popuplate Select from JSON (Solved)

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

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

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

    {
    	"c2array" : true,
    	"size" : [3, 1, 1],
    	"data" : [[["Item1"], ["Item2"], ["Item3"]]]
    }[/code:3azy3w3x]
    
    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.
  • Try Construct 3

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

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

    {
    	"c2array" : true,
    	"size" : [3, 1, 1],
    	"data" : [[["Item1"], ["Item2"], ["Item3"]]]
    }[/code:1yh3p4sx]
    
    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.

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