Windows Trying to copy a ds_map including another map

Discussion in 'Programming' started by SuperRonanCraft, Sep 11, 2019.

  1. SuperRonanCraft

    SuperRonanCraft Member

    Joined:
    Dec 29, 2018
    Posts:
    12
    So im currently trying to make a weapon that has default/global values, but then the weapon can also contain other special things such as custom step events and things of that nature. This is all stored in a ds_map with enum values associated as the keys. Now, I want to be able to copy the ENTIRE ds_map of the weapon, and also a seperate copy of the custom weapon map. So the map kinda looks like this.
    ```
    var type = WEAPON.ROCKET;
    var r_map = scRocketGetMap(type); //Returns a new map of the weapon
    var map = ds_map_create();
    map[? WEAPON_MAP.TYPE] = type;
    map[? WEAPON_MAP.SPEED] = 5;
    map[? WEAPON_MAP.MAP] = r_map;
    weapon_map = map;
    ```
    When a weapon spawns, it copies the "r_map" and deletes it. But it also deletes the original map of the player, which is the weapon_map variable.

    Any idea how to copy over a map within a map?
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,451
  3. SuperRonanCraft

    SuperRonanCraft Member

    Joined:
    Dec 29, 2018
    Posts:
    12
    I am making a new map and copying it over. But I need to make a copy of the map inside of the map im trying to copy, but instead its just referencing the original map
     
  4. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,450
    Are you trying to copy a parent map and all of the child maps inside of it, or make a copy of a single child map inside a parent map?
     
  5. SuperRonanCraft

    SuperRonanCraft Member

    Joined:
    Dec 29, 2018
    Posts:
    12
    mhh, trying to copy the parent map with a child map inside of it, yes
     
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    953
    If you use
    Code:
    var type = WEAPON.ROCKET;
    var r_map = scRocketGetMap(type); //Returns a new map of the weapon
    var map = ds_map_create();
    map[? WEAPON_MAP.TYPE] = type;
    map[? WEAPON_MAP.SPEED] = 5;
    ds_map_set_map(map, WEAPON_MAP.MAP, r_map);
    
    You could try using
    Code:
    weapon_map = json_decode(json_encode(map));
    ds_map_destroy(map);
    
    It depends on the child map being registered as a map, rather than just assigning a numerical id value that has no inherent meaning. Caveat, this change assumes that scRocketGetMap() returns a new map that can be destroyed when map is destroyed. If that is not correct, you need to use ds_map_copy() for it.
    Code:
    ds_map_set_map(map, WEAPON_MAP.MAP, ds_map_create());
    ds_map_copy(map[? WEAPON_MAP.MAP], r_map);
    
    If your data structure grows complex you might want to look at using @FrostyCat's JSON Toolkit.
     
    Last edited: Sep 11, 2019
  7. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    686
    @chamaeleon it's ds_map_add_map .

    As long as you declare the inner maps as such in the parent, the json method could work and is fairly simple. Unfortunately ds_map_copy does not consider nested structures even when marked this way.
     
  8. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    953
    Of course it is. Doh. A small brain glitch while typing in the forum instead of trying it in gms and copy paste. Yeah, the copy function does not handle deep copy of embedded maps and lists for whatever technical reason. JSON Toolkit does have a function that indicates it does deep copy however I have not used it myself.
     
  9. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,451
    You don't have to use it yourself, you've already guessed what it is. The deep copy function in JSON Toolkit is identically json_decode(json_encode(map)) (source), just as you said in post #6. It is the best GML-level approximation to a true deep copy that I know of.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice