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

Legacy GM [Solved]Storing a ds_grid in an ini file

  • Thread starter Quinnlan Varcoe
  • Start date
Q

Quinnlan Varcoe

Guest
Is there any way to store a ds_grid in an ini file? I have tried this way:

scr_savegame
Code:
if (file_exists("Save.sav")) file_delete("Save.sav");
ini_open("Save.sav");
for (var yy = 0; yy < obj_inventory.height; yy++;) {
    for (var xx = 0; xx < obj_inventory.width; xx++;) {
        ini_write_real(string(xx), string(yy), obj_inventory.box[# xx, yy]);
        ini_write_real(string(xx), string(yy), obj_inventory.count[# xx, yy]);
    }
}
ini_close();
scr_loadgame
Code:
ini_open("Save.sav");
ds_grid_clear(obj_inventory.box, item.none);
ds_grid_clear(obj_inventory.count, item.none);
for (var yy = 0; yy < obj_inventory.height; yy++;) {
    for (var xx = 0; xx < obj_inventory.width; xx++;) {
    obj_inventory.box[# xx, yy] = ini_read_real(string(xx), string(yy), 0);
    obj_inventory.count[# xx, yy] = ini_read_real(string(xx), string(yy), 0);
    }
}
ini_close();
 
A

Aura

Guest
Convert the map's data into a string and write that to the INI file.

http://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds maps/ds_map_write.html

When needed, read the string from the INI file and convert it back to a map.

http://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds maps/ds_map_read.html

There are other viable options as well. The ds_map_secure_*() functions are pretty good at what they do.

http://docs.yoyogames.com/source/da...ta structures/ds maps/ds_map_secure_save.html

http://docs.yoyogames.com/source/da...ta structures/ds maps/ds_map_secure_load.html
 
Q

Quinnlan Varcoe

Guest
Thanks!

However I am still getting some issues.

scr_savegame
Code:
if (file_exists("Save.sav")) file_delete("Save.sav");
ini_open("Save.sav");
var box_string;
var count_string;
box_string = ds_map_write(obj_inventory.box);
count_string = ds_map_write(obj_inventory.count);

ini_write_string("Saved", "0", box_string);
ini_write_string("Saved", "1", count_string);
ini_close();
Upon running this script:

Code:
___________________________________________
############################################################################################
ERROR in
action number 1
of  Step Eventobj_model
for object obj_saver:

Data structure with index does not exist.
 at gml_Script_scr_savegame (line 5) - box_string = ds_map_write(obj_inventory.box);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_savegame (line 5)
called from - gml_Object_obj_saver_CollisionEvent_4_1 (line 1) - scr_savegame();
 
A

Ariak

Guest
Simply use ds_grid_write and ds_grid read to convert to string.

This can then be saves in a ds_map with secure save for example.

Loading is then only openening the map with ds_secure_load, and convert to a previosuly created
Code:
my_grid=ds_grid_create(ww,hh) // make it easy on yourself and grab ww,hh from the map (seperate keys)
ds_grid_read(my_grid,map[? "grid"]); // returns decoded grid - dont forget to destroy later on ;)
https://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds grids/ds_grid_write.html


EDIT: to answer your question directly im gonna copy paste from the official documentation:

Code:
ini_open("Save.ini");
ini_write_string("Save", "0", ds_grid_write(mygrid));
ini_close();
As a sidenote: you do can do the same thing with ALL datastructures - they have read / write to string functions. No arrays, though at least not directly (!), there are ways to do it...
 
Last edited by a moderator:
Keep in mind too, if I'm not mistaken, ini files have a size limit that can easily be exceeded by a fairly large grid. Using ds_map_secure is almost as easy an using ini files and doesn't have the size limit.
 
Q

Quinnlan Varcoe

Guest
Thanks everyone,

I decided to go with the secure load and save feature. However now I'm receiving this error report.
___________________________________________
############################################################################################
ERROR in
action number 1
of Draw Event
for object obj_inventory:

Data structure with index does not exist.
at gml_Object_obj_inventory_DrawEvent_1 (line 14) - draw_sprite(spr_items, box[# xx, yy], tx, ty);
############################################################################################
 

YanBG

Member
Didn't your original method(with for loops) work? I'm using that, is it unoptimized coding or...?
 

YanBG

Member
Did you open the ini(with text editor) and see if all is written in there? If all is in there then the save part works and it has to be the load.
 
Q

Quinnlan Varcoe

Guest
Thanks, this helped a bit more, I was confused on why the secure save functions werent working!
 
Top