• 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!

DS_Map Saving (Best practices) [SOLVED]

Ax209

Member
Hey everyone. I'm looking for some advice, if anyone has any thoughts on this. It's regarding the best way to save info to a json/ds_map at multiple points of the game, and saving to different keys.

So, I think most of us came from saving to .ini files, right? I certainly did. A good thing about .ini saving, is that I could open the ini at any point, and save new information, and that would retain all of the previous information stored in there.

So I set up a really basic ds_map save/load system just to test if this could happen with json/maps. I saved information to variables 1, 2 and 3. I then loaded them from the json file and I could see them perfectly, just like the rest of the game. However when i went to save data for variables 4, 5 and 6 into the same file, although they loaded fine, I then tried to load data for variables 1, 2 and 3 and that data had disappeared.

My game saves different info at different points of the game, so for example at the end of each level, I save only the data I need and then load the next level, calling from the data on that file to tell me who was in the party, hp, gold etc. But I use a different save file as, say, the main controller's data. Or the unlocked chatacters to choose at the start of the game.

controllerdata.sav
characters.sav
endlevel.sav

and all of these are accessed at different times. Truth is, I've got about 15 different save files in my folder and it gets messy. Ideally I'd like to keep them all in a single save file. Is there any way I can access a map throughout the game, and only destroy it upon cleanup (end of level/room/game end)? Most tutorials say you have to destroy your map straight away.

What if there was a way I could open the map/file at the start of the game, and only destroy it when the game ends? So long as it was only being opened once and closed once, it should be that bad should it?

Or is there a way i can open a whole map from a json, change the data, and then save, but save ALL of the keys and their data, not just the ones I've used/written.

I'm sorry, I'm really still confused by ds_grids. I'm sure they're not confusing at all, but I have a very specific learning style and somethings, some things just throw me completely off. That's why it takes me forever to write the smallest piece of save code with ds_maps and json.

Many thanks in advance :)

Update: So playing around with seperate code than my game, I've realised that there is a way you can add to a json just by keeping the map open, then updating it and saving the file whenever needed.

I'm gonna try and clean everything up and implement a save system this way, by keeping a map open from the beginning till cleanup event. If someone replies with "OH NOOO, DON'T KEEP A MAP OPEN" then I'll change it, hahaha.
 
Last edited:

CloseRange

Member
@Ax8472
Most tutorials say you have to destroy your map straight away.
I think either you understood wrong or they have misspoken.
You should destroy your map as soon as it's not longer needed.
Keeping a map open throughout the game is completely fine as long as you need it.
The problem comes when you:
1: overide the variable storing the ds map without first deleating it
this is because game maker doesn't fully clear the data from storage until it's been destroyed so even if you overide it, it's still eating up memory
2: forget to destroy it all together
this is just generally bad practice and forgetting to delete it can cause problems down the line (even in the game end event you should destroy them)
3: constantly create new ds_maps
ds_map_create is a fairly heavy function and takes time to process. So weather you remember to delete the map or not, you shouldn't do it in the step event/draw event or rapidly in quick succession
creating/saving/deleating ds maps is something that should be only during load sessions (this means before the game starts, after level transitions, or whenever you'd normally have a load screen)
 
@Ido-f It's not necessary. Windows handles that on the OS level, so even if GMS were screwing up and not clearing it's memory when the program shut down, Windows would jump in and handle that 💩💩💩💩 when it clears the memory allocated for the program. I think there used to be a bug where it was possible for the GMS IDE itself to get bogged down with memory leaks because when you ran the game from within the IDE, data structures weren't properly getting cleared if you didn't clear them (even on Game End), but this is no longer a thing. So don't feel bad that you're not clearing on Game End (though, if you are clearing from the Clean Up event, that should get triggered on Game End anyway).
 

CloseRange

Member
Yes it's technically not necessary but it's good practice just as you don't need to use || and && or use semi colons or () on if statements, it's still a good habit to get into:
... However, it's good practice to still implicitly destroy them. If you were to one day decide to rework how your game works and say, implement your own "game_restart" function, for example, its really easy to forget about these things and find yourself with a memory leak that's difficult to identify.
I think every computer science teacher who I've worked with has told me to clear instances before the program ends even though the OS usually deals with it. I don't know tbh maybe they just expect us to all one day write our own operating system and want us to be prepared. I guess you don't gotta worry about it.
 
It's more that the Clean Up event should handle things like writing your own game_restart function. So if you're using the Clean Up event to destroy your data structure, which you should be, the destruction of the data structure is implicit in the destruction of the instance. If you're writing your own game restart function, putting a data structure destroy call in Game End won't help with anything, as that only really applies to the actual game_end() function (or closing of the window, etc, and is only valid on a few platforms). AFAIK, the only real point of the Game End event is to save data if you want to (there's probably some creative uses beyond that, but that's the main one).
 
H

Homunculus

Guest
As others have already said, you can definitely keep your ds_maps throughout the game. @CloseRange explained quite well why destroying data structures is essential and needs extra care, but there's nothing wrong with keeping them in memory as long as you need (and in fact, in this case it is probably the best option).
If you don't already do it, you may also consider having a dedicated object (or better, a more generic game controller object) for holding those map references instead of global variables. This helps freeing / destroying them in case you want to implement a game restart routine or multiple save slots by using a clean up event or similar.

I don't see a problem with having multiple save files as long as the information they hold belongs to different aspects of your game and you close the file handle whenever you read or write into them, but you can definitely merge all or part of the data into a single file by nesting data structures. You need to use ds_map_add_map / ds_map_add_list and the other related functions though, simply storing the ds_map or ds_list id into the main data structure will not work.
 
Last edited by a moderator:

Ax209

Member
Thanks for your help everyone. With all of that advice (which is exactly what I needed) I'll go and see what I can come up with. You're all absolutely fantastic for taking the time to respond to me, I appreciate it!
 
Top