Json array in array

  • I want to make a json with the coordinates of a bunch of instances.

    Basically "for each" instance set the value at key to array of the x, and y.

    I can do this easy with a csv of the values, but I'm fairly new to json, and could use some help on setting that up.

    Thanks.

    Tagged:

  • There are several ways to do it I guess, here is one:

    For each Sprite
    	JSON Set "coordinates"&Sprite.IID to array size 0
    	JSON Push back Sprite.X to "coordinates"&Sprite.IID 
    	JSON Push back Sprite.Y to "coordinates"&Sprite.IID 
    
    

    This will add a bunch of "coordinatesNN" keys, each containing an array with x,y values.

  • Thanks, but what I need to do is

    * On function 'jsonstart'

    -> JSON: Set "sprite" to array size 0

    ----+ System: For each Sprite

    -----> JSON: Set "sprite.xy" to array size 0

    -----> JSON: Push back Sprite.X to "sprite.xy"

    -----> JSON: Push back Sprite.Y to "sprite.xy"

    That should allow me to use the for each in json

    But that's not working.

  • Ok apparently set array "sprite.xy" is overwriting itself.

    I can't push on the back of array "sprite" a new array of "xy" because I can't create one there without naming it something else.

    I can read a json with an array nested, the "books" demo for example, but I can't write one.

    Ashley what do I do?

  • Using an alternate method...

    * On function 'jsonstart'

    ----+ System: For each Sprite

    -----> JSON: Set "sprite."&loopindex&".xy.0" to Sprite.X

    -----> JSON: Set "sprite."&loopindex&".xy.1" to Sprite.Y

    Using the resulting json

    + AJAX: On "load" completed

    -> JSON: Parse JSON string AJAX.LastData

    ----+ JSON: For each entry in "sprite"

    -----> System: Create object Sprite on layer 0 at (JSON.Get("sprite."&JSON.CurrentKey&".xy.0"), JSON.Get("sprite."&JSON.CurrentKey&".xy.1"))

    This seems pretty convoluted for the inexperienced

    Also loopindex would be nice to have for the json loop.

    Saving, and loading arrays of objects should be much easier imo.

    Gui's, composite sprites etc are a basic necessity.

  • You are saving a simple collection of objects properties, so this shouldn't be as complicated.

    Here's how I'd do it : drive.google.com/open

    You could save a lot more properties than x and y, but for the purpose of my exemple, I saved only those.

  • Thanks, thats a lot cleaner.

    Still the use of the Json object is not that intuitive to me.

    I mean no matter what you're going to have to slap some strings together.

    Seems like nesting an array shouldn't require the extra steps.

  • Well to be fair, JSON as a whole can be described as a bunch of strings slapped together. :P

  • You can also add all x/y coordinates into one array.

    You will just have to read values back in pairs. I couldn't find the way to add 2D-array to JSON.

  • Maybe add a delimiter and parse it that way.

    Maybe better with that method would be the save state. A little bloated, but you would be able to get things like instance variables saved.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • There's no need for that. My method uses an array of javascript objects. You can store pretty much anything you want.

    I expanded my exemple so it can save objects type and 3 random variables.

    Used well, JSON is the better alternative for all you data needs.

  • Well I should have said the asJson. The save state wouldn't really work in a lot of situations.

    Actually I got to thinking about it, and Spriter would be interesting to try for this.

    It at least has a few of the same features.

  • Oh yeah, right, asJson could certainly work. You'd still have to save the object type somewhere if you wanted to save multiple types in a single array. Or you could also use one array per object types.

    Updated my file yet again.

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