Game Maker – Save and load a key-value pair

Code snippet for saving a key-value pair in gamemaker. Data is saved in the file data.dat. Works for all platforms.

Saving:

mKey="myKey";
mValue="myValue";
mMap = ds_map_secure_load("data.dat");
if (mMap==-1){
   mMap=ds_map_create();
} 

if (!ds_map_exists(mMap, mKey)){
   // add the key-value 
   ds_map_add(mMap, mKey, mValue);
} else {
   // replace the value for the key
   ds_map_replace(mMap, mKey, mValue);
}

ds_map_secure_save(mMap, "data.dat");
ds_map_destroy(mMap);

Loading:

mKey="myKey";
mValue="default value";

mMap = ds_map_secure_load("data.dat"); 
if (mMap==-1){
mMap=ds_map_create();
}
if (ds_map_exists(mMap, key)){ mValue=ds_map_find_value(mMap, key); } ds_map_destroy(mMap);

Leave a Reply