• 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 Saving nested data structures to file

J

JapanGamer29

Guest
Hi everyone. I have a puzzle game with an Undo feature that works by taking a snapshot of the game after each move and putting it on a stack.

By 'snapshot", I mean a ds_map that holds all the data I need.

DS_STACK
---- DS_MAPS, with each one containing...
---- ---- two integers
---- ---- a DS_GRID with integers in each cell
---- ---- a DS_LIST of...
---- ---- ---- DS_MAPS , where each map contains the x, y coordinates of each puzzle piece

You might think it looks messy, but it all works nicely. I've even managed to save the stack to a text file so a player can save mid-game, then come back later and continue with all undo actions still possible.

What worries me is that to save this to file, I had to convert the data structures to strings using, for example, ds_list_write(). This means I can easily generate text files 2.5 million lines long that weigh 2,500KB.

What do you think? Is a file of that size too much? It loads fast enough (0.07 seconds), but millions of lines seems crazy to me. Is it crazy? Or is it quite common in game dev? I'm still very much a beginner here.
 
Last edited:

devKathy

Member
If file size is a concern, possibly something to think about is only storing the start state and the moves made.

If the moves have a predictable enough effect (I'm thinking like a sliding tile puzzle), it wouldn't be challenging to produce all the intermediate game states that logically follow from them. :)
 
J

JapanGamer29

Guest
@devKathy, that's a really good idea. I hadn't even considered approaching it that way. I have a Shuffle feature which would complicate it a bit, but it should still be possible.
 

kburkhart84

Firehammer Games
I don't think there is any issue with a millions of lines of text in a file that is only a few MBs. You said it loads fast enough....its all just bytes as far as the computer is concerned, and there are things that take up so much more space than that, likely just a small handful of your sprites, or maybe a single one, depending on your game.
 

Niften

Member
json_encode and json_decode is perfect for stuff like this - when saving, put the ds_grid_write string in the key where the ds_grid should be, and then use json_encode to save it into a json file. Then, when loading, all you would have to do is json_decode and it would create the same map for you (you would just need to create the grid again from the string that you saved). You would also have to replace the ds_stack with a ds_list (this might be a deal-breaker), but json files are clean solutions for saving nested maps/lists.
 
J

JapanGamer29

Guest
I finally settled on replacing the small ds_maps containing x,y coordinates with simple "x,y" strings, and retrieving them with a split_string() function. It made the text files 10x smaller. :)

I kept the stack, but json_encoded the larger maps within it.

Thanks for all the suggestions.
 
Top