SOLVED Saving files (i need to save a grid)

Darnok

Member
hey so i have leaderboards in my game and i surly sucks if you get a good time close the game and then you open it again the leaderboards are blank. so i wanted to save the grid i store everything in i watched a bit around and so but i dont think i understand it.
i found this
GML:
saveStruct = {
    name: "Josh",
    hp: 3
};

// SAVING
var f = file_text_open_write("save.dat");
file_text_write_string(f, json_stringify(saveStruct));
file_text_close(f);

// LOADING
var f = file_text_open_read("save.dat")
saveStruct = json_parse(file_text_read_string(f));
file_text_close(f);
and was like if i replace the savestruct with my global.grid_levels (the grid i save the leaderboards in) it sould work so i made 2 scripts
Code:
function lb_saving(){


// SAVING
var f = file_text_open_write("save.dat");
file_text_write_string(f, json_stringify(global.grid_levels));
file_text_close(f);

show_message("lb saved")



}
and
Code:
function lb_loading(){

// LOADING
var f = file_text_open_read("save.dat")
global.grid_levels = json_parse(file_text_read_string(f));
file_text_close(f);

show_message("lb loaded")

}
but i dont think it works because if i save something close the game and try so load it the script dose run but nothing happens would like to have some help here thx ;)
 
Last edited:

FrostyCat

Redemption Seeker
When you say "save a grid", do you mean a DS grid? If you do, that won't work with json_stringify because DS data structures work on numeric IDs. Your code basically reduces to something like json_stringify(0), which has zero pertinent or persistable information.

If you want to do this with JSON, you have to rewrite your leaderboard system to use arrays and structs instead of DS data structures. Here is a quick example:

Setup:
GML:
function lb_setup() {
    global.leaderboard = [
        { name: "Alice", points: 583 },
        { name: "Bob", points: 492 },
        { name: "Caitlyn", points: 371 },
        { name: "David", points: 264 },
        { name: "Evelyn", points: 180 }
    ];
}
Inserting a score:
GML:
function lb_insert(_name, _score) {
    array_push(global.leaderboard, {
        name: _name,
        points: _score
    });
    array_sort(global.leaderboard, function(a, b) {
        return b.points - a.points;
    });
    array_pop(global.leaderboard);
}
Loading:
GML:
function lb_load(_fname) {
    var f = file_text_open_read(_fname);
    global.leaderboard = json_parse(file_text_read_string(f));
    file_text_close(f);
}
Saving:
GML:
function lb_save(_fname) {
    var f = file_text_open_write(_fname);
    file_text_write_string(f, json_stringify(global.leaderboard));
    file_text_close(f);
}
 

Darnok

Member
is there a way to keep the ds grid (dont have to be a JSON can be anything if it works)? because i gave 1 grid global.grid_levels (100,3) were i have 100 grids ([i,0] = the leaderboards) inside it works awsome and i would like to keep this if this is way harder i will try what you sad but if its about the same i would prefer that
thx so far tho ;)

GML:
for(i=0;i<ds_grid_width(global.grid_levels);i++;)
{
   
    ds_grid_set(global.grid_levels,i,0,ds_grid_create(4,11));
   
    for(j=0;j<11;j++;)
    {
        global.grid_levels[# i,0][# 0,j] = "____";//name
        global.grid_levels[# i,0][# 1,j] = 999.99;//time
        global.grid_levels[# i,0][# 2,j] = 0;//hp
        global.grid_levels[# i,0][# 3,j] = 0;//kills
    }
   
   
}
 
Last edited:

kburkhart84

Firehammer Games
is there a way to keep the ds grid ? because i gave 1 grid global.grid_levels (100,3) were i have 100 grids ([i,0] = the leaderboards) inside it works awsome and i would like to keep this if this is way harder i will try what you sad but if its about the same i would prefer that
thx so far tho ;)

GML:
for(i=0;i<ds_grid_width(global.grid_levels);i++;)
{
   
    ds_grid_set(global.grid_levels,i,0,ds_grid_create(4,11));
   
    for(j=0;j<11;j++;)
    {
        global.grid_levels[# i,0][# 0,j] = "____";//name
        global.grid_levels[# i,0][# 1,j] = 999.99;//time
        global.grid_levels[# i,0][# 2,j] = 0;//hp
        global.grid_levels[# i,0][# 3,j] = 0;//kills
    }
   
   
}
You can keep using a grid if you want. But you will have to use either the ds_strings you use with the ds_grid_read/write() functions to get a string you can then send to a file, or you will have to make your own format for it. You can't use the automatic json stuff. The ds_string functions are probably the easiest way to get it going, though many people don't like them for different reasons.
 

Darnok

Member
ok so i now have this

GML:
function lb_saving(){


// SAVING
var f = file_text_open_write("save.dat");
file_text_write_string(f, json_stringify(ds_grid_write(global.grid_levels)));
file_text_close(f);

show_message("lb saved")



}
and
GML:
function lb_loading(){

// LOADING
var f = file_text_open_read("save.dat")
ds_grid_read(global.grid_levels,json_parse(file_text_read_string(f)));
file_text_close(f);

show_message("lb loaded")

}
it dose still not work did i do it wrong ?
 

kburkhart84

Firehammer Games
I don't think you should be calling json_stringify() nor json_parse() at all. Those are for converting data into json strings....you already HAVE strings and should just save that directly.
 

chamaeleon

Member
Relying on the read/write functions for ds data structures is not recommended for save systems as the format could change in an incompatible manner with updated GMS versions (although YYG does everything they can to avoid this, I'm sure, it is not a theoretical problem, it happened this year due to not bumping a version flag when changing the serialization). They are also needlessly large in size (not always an issue but some other person on this board spent a lot of time implement compression for the strings or data structure content, details escape me).
 

kburkhart84

Firehammer Games
Relying on the read/write functions for ds data structures is not recommended for save systems as the format could change in an incompatible manner with updated GMS versions (although YYG does everything they can to avoid this, I'm sure, it is not a theoretical problem, it happened this year due to not bumping a version flag when changing the serialization). They are also needlessly large in size (not always an issue but some other person on this board spent a lot of time implement compression for the strings or data structure content, details escape me).
This is a valid point(and what I was getting at earlier in the thread). The OP doesn't want to convert away from grids though, meaning they either use those string functions, or they make their own strings(or file format using binary).
 

Darnok

Member
so i tryed 2 things
GML:
function lb_saving(){

/*
// SAVING
var f = file_text_open_write("save.dat");
file_text_write_string(f,ds_grid_write(global.grid_levels));
file_text_close(f);
*/
show_message("lb saved")


ini_open("Save.ini");
ini_write_string("Save", "0", ds_grid_write(global.grid_levels));
ini_close()


}
GML:
function lb_loading(){
/*
// LOADING
var f = file_text_open_read("save.dat")
ds_grid_read(global.grid_levels,f);
file_text_close(f);
*/
show_message("lb loaded")


ini_open("Save.ini");
ds_grid_read(global.grid_levels, ini_read_string("Save", "0", ""));
ini_close()

}
none of it worked what did i do wrong this time ? thx ;)

edit: if i want to use the JSON i have to dont use ds grids but arrays are ok right ? so i can just build my self some grids with lots of arrays right ?
GML:
global.array_levels = array_create(100)
for(i=0;i<100;i++;)
{
    global.array_levels[i] = array_create(3)
    for(j=0;j<3;j++;)
    {
        global.array_levels[i][j] = array_create(4)
        for(k=0;k<4;k++;)
        {
            global.array_levels[i][j][k] = array_create(11)
        }
    }
}
 
Last edited:

kburkhart84

Firehammer Games
none of it worked what did i do wrong this time ? thx
It seems to me that you need to learn some debugging skills and figure it out. I don't see anything exactly wrong with that code...but I don't any code that is actually calling those functions either. You need to do some basic stuff like verifying that these functions are being called at the right times. You should also check in the folder if the file is being create. Dig deeper, and figure out which part is actually failing, if it is the loading, the saving, or possibly you are overwriting with defaults even if the save and load is successful. The debugger is the best tool for checking those kinds of things. It lets you directly see the contents of things, including variables, structs, and yes, even ds_grids.
 

Darnok

Member
thx i have done it in a other way i just need to figer out how to sort the array right (made a new post if you want to look into it ) but at least the saving and loading there workes
 
Top