ds_map_add

Adds the given value and associated key into the map.

Syntax:

ds_map_add(id, key, val);


Argument Description
id The id of the map to add to.
key The key of the value to add.
val The value to add to the map.


Returns: Boolean


Description

This function should be used to add sets of key/value pairs into the specified ds_map. You can check this function to see if it was successful or not, as it may fail if there already exists the same key in the ds_map or you specify a non-existent ds_map as the id of the map to add to. Both keys and values can be made up of either integers or strings, so all of the following are acceptable:

ds_map_add(map, 5, 1);
ds_map_add(map, "level", 100);
ds_map_add(map, 89, "hello world");
ds_map_add(map, "fish", "good");

You can also add to a map using the accessor "?", as shown below:

map[? 5] = 1;
map[? "level"] = 100;
map[? 89] = "hello world";
map[? "fish"] = "good";



NOTE: Unlike other data structures in GameMaker Studio 2 this key will not go to the start (nor the end) of the ds_map, but rather it will just go into the ds_map somewhere.


Example:

inventory = ds_map_create();
ds_map_add(inventory, "hp potion", 1);
ds_map_add(inventory, "gold", 100);

This will create a new map, assigning its id to the variable "inventory". It then adds two new keys to the map, "hp potion" and "gold", and sets their initial values as 1 and 100.


Back: DS Maps
Next: ds_map_clear
© Copyright YoYo Games Ltd. 2018 All Rights Reserved