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

converting Json maps into regular maps

ophelius

Member
Ok so I discovered json maps aren't quite like regular maps. Here's what I mean:

I make a map and add a key/value to it, then make a copy, destroy the original, and I can access the new map no problem:
Code:
    var Map = ds_map_create();
    ds_map_add(Map, "test", 10);
    
    var MapCopy = ds_map_create();
    ds_map_copy(MapCopy, Map);
    
    ds_map_destroy(Map);
    
    //I can access the copy map no problem
    MapCopy[? "test"] = 20;
Importing json data is a different story:
Code:
    var jsonMap = json_decode(buffer_read(buffer_load("File.json"), buffer_string));
    //at this point, jsonMap looks and behaves like a normal ds_map, I can navigate it in the debugger like normal

   //create a new map to copy the jason map to it
    var MapCopy = ds_map_create();

   //copy the json map to the new map
    ds_map_copy(MapCopy, jsonMap);
    
    //destroy the original
    ds_map_destroy(jsonMap);
    
    //This map is not accessible, MapCopy is still only a reference to the original
    MapCopy[? "test"] = 500;  //ds map not found
I'd like to understand how I can convert json maps into normal ds maps where I can copy the map into a new one and delete the original without it affecting the copy. Any help would be appreciated. Thanks
 

ophelius

Member
Ok, I looked through the manual and apparently I should be using json_parse instead of json_decode, which will turn all that data into a struct instead of a ds_map. Lists that would have been in the ds_map would be arrays inside the struct. Now it's easy to copy, you just assign the struct to a new variable. I tried "deleting" the old struct by setting it to 0 and the new struct keeps everything intact.
 
Top