GameMaker Best way to save nested arrays to file?

I've set up nested arrays for my Zelda-like game to track information related to the maps- what rooms you've entered, various gamestate flags, etc. They're essentially three arrays in one- an array containing the rooms, inside an array containing the possible floors of the dungeon, inside an array containing the possible dungeons of the game. So I want to save this information to a file, but I'm not sure what the best way to go about it is- I've seen tons of suggestions of using ds_grids, but those are for 2d arrays- what I have here is basically a 3D, X axis for dungeon, Y axis for floor, Z axis for the room. (This sole fact is why I'm using an array, not a grid, a list, or a map.) So what's the efficient way to store all this into a file? Should I separate each level of array out into a separate list?
 

Nidoking

Member
You'd probably be doing either a list of grids, or a list of lists of lists. The general simple format for such a file is something like:
Number of dungeons
Number of floors in dungeon 1
Number of rooms on floor 1
Rooms - however many entries
Number of rooms on floor 2
Rooms - however many entries
etc.
 
You'd probably be doing either a list of grids, or a list of lists of lists. The general simple format for such a file is something like:
Number of dungeons
Number of floors in dungeon 1
Number of rooms on floor 1
Rooms - however many entries
Number of rooms on floor 2
Rooms - however many entries
etc.
The last time I tried to make a list or map containing lists, it massively broke down almost instantly- suddenly removing two lists that existed prior. I'm not sure nesting lists in lists is a good idea after that...
 

Nidoking

Member
I suggest that something was wrong with your implementation rather than the concept itself, but without the actual code, I can't provide any solid examples. You could do a list of different-sized 2D arrays, or you could make a list of some Dungeon object that has in it a list of Floor objects that then have lists of Rooms, so it's a list of instances instead of a list of lists. That gives you more flexibility to manage data structure, at least.
 

Binsk

Member
If you've already implemented something as an array then there is no need to convert it to a data structure.
  1. Do you know how to loop through your arrays?
  2. Do you know how to write to a file?
Problem solved.

Just tackle the problem in small bites.
  • Figure out how to loop through every element of your nested array, print it to the debug console or something.
  • Figure out how to write a text file (or binary file). Change from printing to debug to outputting to the file.
  • Figure out how to read the data back in and print it to the debug console.
  • Figure out how to rebuild your arrays and store in these instead of printing to the debug console.
If your arrays are always the same size, this is super simple. If your arrays have varying sizes then you can just print an extra bit into your file before each array that says how big it needs to be or something. If you don't like using text files, output into a buffer instead or something similar.

If you are set on using a list then you can always flatten your array, pop it into a list, then save the list. Again, if you have varying size arrays you'd need to throw in some kind of marker or something so you can rebuild it properly. Not super hard if you just go a step at a time.

If you still can't get your mind around it, write a dummy project that does this and start with doing it to a 1D array. Figure out how to make a nice clean loop that does all the work for you. When you've got it down, expand it to a 2D array, then a 3D.
 

Joe Ellis

Member
You basically need to read through the arrays in a hierarchical way, so arrays that contain sub arrays need to first write their info then move on to the next lower array. That's probably not that helpful. I wanted to write some example code but I don't know at all what you would be writing. Could you tell me in more detail what the arrays are used for?
 
Unless you're planning on using the HTML5 export, the simplest way to save deeply nested arrays is to do the following:
1 - Create a temporary ds_list
2 - Add the root array to index 0 of the ds_list
3 - Use ds_list_write to cover the list, and the entirety if you nested array into a string value.
4 - Save the string.
5 - Done.

Later, to retrieve the array, do the opposite:
1 - Create the temporary ds_list
2 - Load your saved data string
3 - Use ds_list_read to convert the string back to data
4 - Extract your nested array out of index 0
5 - Done.

... however, do remember to remove the temporary list from memory after you're done. It also should be noted that this trick May not work with the coming updates to GML, though I truly do hope it will still, as i make use of it rather extensively.
 
Top