How to Save DS Grids as .ini in Gamemaker Studio 2

Wondering how to save and load DS grids as an .ini file? Luckily, Gamemaker makes it very easy with it’s properietary language, GML.

Saving ds grids

// Save the grid
ini_open("filename");

ini_write_string("Grids", "this_grid", ds_grid_write(grid));

ini_close();

This will save our grid, creatively named “grid”, into an .ini file.

If your grid’s width and height aren’t constant, you can save it’s width and height like so:

// Save the grid
ini_open("filename");

ini_write_string("Grids", "this_grid", ds_grid_write(grid));
ini_write_real("Grids", "this_grid_width", ds_grid_width(grid));
ini_write_real("Grids", "this_grid_height", ds_grid_height(grid));

ini_close();

Loading ds grids

So now we’ve got the grid with it’s width and height all saved up. Now, when we want to load the grid, we first make our grid (and if the width and height are not constant, we’d use those saved values here) and we’d fill it with information using the function ds_grid_read like so:

/// Load the grid

// Open the file
ini_open("filename");

// Get the grid's width
var width = ini_read_real("Grids", "this_grid_width", 0);
var height = ini_read_real("Grids", "this_grid_height", 0);

// Make a grid
grid = ds_grid_create(width, height);

// Fill the grid with ds_grid_read
ds_grid_read(grid, ini_real_string("Grids", "this_grid", ""));

// Close the file
ini_close();

Boom. We’re done! Now, the first block saves grid to an .ini file, and the second block creates grid from that .ini file.

About The Author

Mike Miller
Mike Miller Since the age of nine, Mike Miller has been coding his own games, visualizations, and other technological projects.

Comments