• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker DS map confusion

S

samueljbfrye

Guest
Hey everyone, this feels like a ridiculously small thing to be posting about, but I keep staring at the pages for DS map functions and I really don't understand what I'm missing! I'm (reasonably) familiar with GML but have never done anything that's used DS maps or save files before.

To get to grips with how they work I just made a project with a single room, a single object and the following events:

Create:
Code:
if file_exists("save.dat")
{
    save = ds_map_secure_load("save.dat")
    counter = ds_map_read(save,"counter");
}
else
{
    counter = 0;
    save = ds_map_create();
}
Draw:
Code:
draw_text(room_width/2,room_height/2,counter);
Spacebar press:
Code:
counter += 1;
Enter press:
Code:
ds_map_replace(save,"counter",counter);
ds_map_secure_save(save,"save.dat");
The idea being that I could load the room, advance the counter, save with enter and close and open the game to check if the save and load had worked. It IS creating a save.dat file in the localappdata project folder, but, when I restart the counter is just zero. Any help would be very much appreciated. I'm sure I'm just overlooking/misunderstanding something simple...

Thanks in advance for any help!
Sam.
 

2Dcube

Member
I've never used ds_map_read, and it doesn't seem to return anything looking at the help: https://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds maps/ds_map_read.html
which is very strange to me. It seems to "read something into the map" which sounds more like writing to me...

To get something out of a map I generally use the "?" accessor like so
Code:
counter = save[? "counter"];
Similarly you can overwrite the value with
Code:
save[? "counter"] = counter;
or even just increase it
Code:
save[? "counter"] += 1;
 
S

samueljbfrye

Guest
I've never used ds_map_read, and it doesn't seem to return anything looking at the help: https://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds maps/ds_map_read.html
which is very strange to me. It seems to "read something into the map" which sounds more like writing to me...

To get something out of a map I generally use the "?" accessor like so
Code:
counter = save[? "counter"];
Similarly you can overwrite the value with
Code:
save[? "counter"] = counter;
or even just increase it
Code:
save[? "counter"] += 1;
Thank you for the swift reply, 2Dcube, this worked perfectly. I don't know how I managed to miss the fact that ds_map_read doesn't really do anything like I assumed it did, but, that clears my confusion up!
 
ds_map_read/write and ds_map_secure_save/load do two very different things, and there's really no need to combine them.
ds_map_write converts a ds_map to a string which can then be saved as a string. ds_map_read converts the string output of ds_map_write to a ds_map.
ds_map_secure_save handles all the saving and encryption for you. It automatically saves to a file and then you can load the map back up again with ds_map_secure_load.
 
S

samueljbfrye

Guest
Thanks for both replies! Just as a cheeky follow up question, while on the subject, I'm unclear on the best way to make use of these maps. Assuming I'm storing lots of variables this way, am I better to:

a) Load up the ds map from save and then use the map's entries AS the game's variables, as in your third example 2DCube , or
b) Load up the ds map and use it to assign values to an object's local variables, and then convert back from those local variables to the ds map when saving?

I think I was assuming that ds maps were a little unstable, like surfaces or something, and that b) was therefore correct. But I probably just read that they should be deleted to prevent leaking memory and over-interpreted!
 
ds_maps are as stable as variables once they're created. Not like surfaces at all. You just need to be aware of deleting them when you no longer need them -- especially if you're dealing with them often -- so as to prevent causing memory leaks.
 
Top