Dumping contents of array back into .ini file?

S

spaghettimaker

Guest
I'm been following Roman's Metroidvania Map Implementation thread and have hit a road block.
The map is working as it should but I'm trying to save the map data (array) to an .ini file at save points.
Below is the array and save point code:

ARRAY
ini_open(global.save_selected_temp)
for (var i = 0; i <= 40; i++)
for (var j = 0; j <= 58; j++)
mapGrid[i, j] = ini_read_real("row" + string(i), string(j), 0);
ini_close();


SAVE POINT
if (file_exists(MAPFILE)) file_delete(MAPFILE);
//Create new map save
ini_open("Map.ini");
ini_write_real("Map","row",ds_grid_write(obj_map.mapGrid));
ini_close();

I'm currently getting the following error message:
ds_grid_write argument 1 incorrect type (array) expecting a Number (YYGI32)


Any suggestions would be greatly appreciated!
 
Last edited by a moderator:

Binsk

Member
1) Look up ds_grid_write in the manual and read the page; all of it.
2) Look up ini_write_real in the manual and read the page; all of it. Make special notes about not only how the functions are intended to be used, but also data types.

That said, in regards to your error it is stating that you are giving the function ds_grid_write an array when it expects a number. This leads to the second issue (and the one you are asking about). mapGrid is not a ds_grid, it is a 2D GM array. A ds_grid is created with ds_grid_create(). Perhaps you did this with mapGrid which, in this case, you are not using the proper accessor when assigning it values (which is the # symbol).

When you have problems like this, your first action should always be to double-check the manual to make sure you are calling / using things correctly. In your case you are not and this in more than one area.
 
S

spaghettimaker

Guest
Is there a way to dump the contents of the 2D GM array back into the .ini file without using a ds_grid?
 

Binsk

Member
In a single one-off function like that? Not that I know of.

You could write your own script that cycles through each value in the array and writes it in some custom format to a string or something. You'd also have to write the script to read the string and re-build the array.

You could also just convert things to use ds_grids instead of arrays. That's really the whole point of data-structures. They give you ways to properly structure your data, go figure.

Not sure how extensive your system is so that's really for you to decide.
 
S

spaghettimaker

Guest
Based off Roman's map implementation how do you think he dumps the array back into an .ini file?
 

Binsk

Member
I don't know his implementation and scanning through his documents didn't reveal an example. You should ask him if you need to find this out.

Just create your own method. There are any number of ways to save data, be it arrays or anything else.

The fact that you have a script to read and form the array in the first place assumes there is already a format defined. You just need to save in that same format. I'm assuming you don't actually know how the format works (which, on that note, never use code if you don't know how it works) but that bit of loading code is your key to saving.

By the looks of it, if you have some array[x, y] that you need to build from the file, your load code is expecting the format:
Code:
[rowx]
y=somevalue
Find a way to write that format into the file based off of your array. You have most of the pieces already.
 
S

spaghettimaker

Guest
Thanks!


//Create new map save
ini_open("Map.ini");
for(var i = 0; i <= 40; i++)
for(var j = 0; j <= 58; j++)
{
ini_write_real("row"+string(i),string(j),obj_map.mapGrid[i,j]);
}
ini_close();
 
To avoid the loop, or if you have nested arrays, you can do something like the following:

Saving:
Code:
var temp = ds_list_create();

temp[| 0] = obj_map.mapGrid;

ini_open("Map.ini");
ini_write_string("Map", "data", ds_list_write(temp));
ini_close();

ds_list_destroy(temp);
Loading:
Code:
var temp = ds_list_create(),
    map = "";

ini_open("Map.ini");
map = ini_read_string("Map", "Data", "");
ini_close();

if (map != "")
{
    ds_list_read(temp, map);
    obj_map.mapGrid = temp[| 0];
}

ds_list_destroy(temp);
... This basically cheats and converts the entire array into a data string by using the ability to read/write data structures back and forth between raw data and string data. This will work with any data structure except ds_maps, as ds_maps will only record the memory address of the array and will point to nothing when reloading.
 
Top