3

I am trying to make a lighting system in Game Maker and to structure the system I want to contain all of the lights in a ds_map. I want that ds_map to be held inside a second ds_map that contains all of the lights in the system

lights {
"l-01": {"x":20, "y":40, "radius":15},
...
}

The question I have regards cleaning up the system: Do I have to iterate through the map destroying all of the sub-maps and then destroy the map, will Game Maker automatically destroy the sub-maps when the light map gets destroyed?

ds_map_destroy(lights); // do the sub maps ("l-01") also get destroyed?

or do I have to do it like this:

var k = ds_map_find_first(lights);
repeat(ds_map_size(lights)){
    ds_map_destroy(lights[? k]);
    k = ds_map_find_next(lights, k);
}

Following along with the first question, if I delete a key will game maker destroy the sub-map

ds_map_delete(lights, "l-01") // will this destroy the map indexed under "l-01"

You may ask: "Why are you using a ds_map to hold a bunch of ds_maps, why not just create a list of maps?

The answer comes from the second question. If I held the maps in a list, and I need to delete one of the maps the list will resize on removal of the map, therefore offsetting all of the other indexed values The second reason is that in Game Maker, ds_maps are much quicker than ds_lists

I hope I have made my question clear and that one of you out there has the answer.

Ctl-F
  • 33
  • 7

1 Answers1

3

If you marked added data structure as list/map (using ds_list_mark_as_map() or ds_list_mark_as_list() or ds_map_add_list() or ds_map_add_map()) then it will be deleted automatically. Otherwise you need delete it yourself.

From documentation about ds_list_mark_as_list():

NOTE: Once a ds_list has had a value within it flagged as another list or map, destroying the list will also destroy the marked lists and maps too. This means that you do not have to manually go through the list contents and destroy the marked data structures individually before destroying the "parent" list.

and ds_map_add_list():

If a ds_map has a list added in this way, destroying the parent map will also destroy the contained lists and free their memory.

If you have doubts you can check, is data structure exists or not, using ds_exists()

Dmi7ry
  • 1,696
  • 1
  • 13
  • 24
  • This doesn't answer the OPs question who was asking for a solution for a map in a map. `ds_list_mark_as_list()` & `ds_list_mark_as_map()` work for lists/maps in lists. – fose Sep 21 '18 at 20:50
  • 1
    I added info about `ds_map_add_list()` and `ds_map_add_map()` – Dmi7ry Sep 22 '18 at 03:17