• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Reading a ds_list in a ds_map from a JSON

A

AlekM

Guest
Hello!

I am trying to extract lists stored within maps in my game, and I keep getting errors.

I have the maps in my game stored as JSON files. The GM manual states that arrays ([]) within a JSON file are saved as ds_lists and objects ({}) are stored as ds_maps.

So here is my JSON file:
Code:
{
    “walls”: [
        { “name”:”wall1”, “x1”:“0”, “y1”:”0”, “z1”:”0”, “x2”:”10”, “y2”:”0”, “z2”:”10”, “texture”:”brick1_1024”, “hrepeat”:”1”, “vrepeat”:”1”, “shader”:”none”, “material”:”stone”   }
    ]
    “floors”: [
        { “name”:”floor1”, “x1”:“0”, “y1”:”0”, “z1”:”0”, “x2”:”10”, “y2”:”10”, “z2”:”0”, “texture”:”grass1_1024”, “hrepeat”:”1”, “vrepeat”:”1”, “shader”:”none”, “material”:”dirt”   }
    ]
}
You can see in the JSON file above the file should be expanded to:
Code:
ds_map default
|_ds_list walls
   |_ds_map walldata
|_ds_list floors
   |_ds_map floordata
So far I have working code that loads in the JSON file, however I have some errors reading from the ds_lists.

This is my code for reading from the ds_map extracted from the JSON:
Code:
var defaultMap = ds_map_find_value(global.mapData, "default");
var walls = ds_map_find_value(defaultMap, "walls");
if (!ds_list_empty(walls)) {                                  //It gives an error here for: ds_list_empty argument 1 incorrect type (5) expecting a Number (YYGI32)
    var size = ds_list_size(walls);
    for (var a = 0; a < size; a++) {
        var current_wall = ds_list_find_value(walls, a);
        d3d_draw_wall(
            ds_map_find_value(current_wall, "x1"),
            ds_map_find_value(current_wall, "y1"),
            ds_map_find_value(current_wall, "z1"),
            ds_map_find_value(current_wall, "x2"),
            ds_map_find_value(current_wall, "y2"),
            ds_map_find_value(current_wall, "z2"),
            back_tex,
            ds_map_find_value(current_wall, "hrepeat"),
            ds_map_find_value(current_wall, "vrepeat")
        )
    }
}
At line 3 it runs into errors telling me that the id argument is incorrect and it expects some sort of number. If I remove that line it gives me the same error at "var size = ds_list_size(walls);" and if I remove that, I get the same error at "var current_wall = ds_list_find_value(walls, a);"

Any idea how I could fix this?
Thanks,
Alek
 
C

CaffeineJunky

Guest
Edit: removed factually false information. I was thinking of the ds_exists function.

Second edit:

After reading the documentation, it appears that your fetching of a value of default from the deserialized json is incorrect. That is only true if the JSON object is a value or an array. Since your object is an array, skip reading the default object.
 
Last edited by a moderator:

FrostyCat

Redemption Seeker
If the top-level structure is an object, there is no default key, json_decode() gives you the starting point directly.
The Manual entry on json_decode() said:
If the JSON to be decoded requires a hierarchy of lists and maps within the central ds_map, these are decoded too and also created for you, using the following rules (note that these rules apply to the top-level structure only):

  • Json is a single value - returns a ds_map with a single entry "default" that is the value
  • Json is an array of objects or values - returns a ds_map with a single entry "default" that is a ds_list of the objects or values
  • Json is an object - returns a ds_map that has the object entries in it
Hence this is how your code should have started:
Code:
var walls = ds_map_find_value(global.mapData, "walls");
 
A

AlekM

Guest
I removed the first line where it refers to the "default" and replaced it with what you have, yet it still returns the same errors. "ds_list_empty(walls)" still returns:
Code:
ds_list_empty argument 1 incorrect type (5) expecting a Number (YYGI32)
 

FrostyCat

Redemption Seeker
Are those smart quotes (the slanted ones) actually from your data? If they are, you need to change them all to straight quotes. Smart quotes have no place in code or machine-intended data.
 
A

AlekM

Guest
ok solved it, it was the slanted quotes. I wrote it within textedit and it made an RTF, which I copy and pasted into notepad. I got rid of all the slanted quotes and now it works.
 
Top