DS Maps

The DS map data structure lets you store key and value pairs, making it an exceptionally useful one.

For example, a character in your game can have a quantity of different items (keys) and for each individual item he can have a number of them (values), like in an RPG where you can have 10 health potions, 5 mana potions and 100 gold. Maps maintain such pairs all together in one place. You can add pairs to the map, search for the value corresponding to certain keys and use some simple functions.

There are a couple of things you should know about maps before you use them, however! Maps are not sorted in any (recognisable) way, meaning that to find a certain key you may have to iterate through the whole thing (which is very slow). There is also no way to hold two keys that are the same, nor can you assign one key two values.

NOTE It is recommended to use structs over DS maps as they have similar features, are easier to use and are garbage collected automatically.

NOTE As with all dynamic resources, data structures take up memory and so should always be destroyed when no longer needed to prevent memory leaks which will slow down and eventually crash your game.

TIP The key isn't limited to strings and can be of any type, including a struct.

Apart from the specific functions listed below you can also use an expression (called an accessor) to add or modify the contents of your DS map. This accessor looks similar to a 1D array with the following syntax:

map_index[? key]

You can find more information, plus examples, from the GML Overview page on Accessors.

Function Reference

General

Serialisation

Loading & Saving

These functions will obfuscate the map and store it in a secure location on the target platform using a file format that means that the final file is not able to be transferred between devices:

JSON

NOTE While these functions permit you to add lists and maps within a map, they are useless for anything other than JSON, and nested maps and lists will not be read correctly if written to disk or accessed in any other way.