Windows Trying to copy a ds_map including another map

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?
 

rIKmAN

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

chamaeleon

Member
mhh, trying to copy the parent map with a child map inside of it, yes
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:
H

Homunculus

Guest
@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.
 

chamaeleon

Member
@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.
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.
 

FrostyCat

Redemption Seeker
JSON Toolkit does have a function that indicates it does deep copy however I have not used it myself.
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.
 
Top