Search code examples
savegame-makergame-maker-language

GML, How do I save what room i'm in to .ini?


I was learning basic saving/loading for an adventure game and came across this error:

I started out simple with just saving X and Y positions with the player along with the room they're currently in.

ini_open("save.ini");

ini_write_string("Player", "Room", room);
ini_write_real("Player", "X", oPlayer.x);
ini_write_real("Player", "Y", oPlayer.y);

ini_close();

However, it throws an error as soon as I try to save.

FATAL ERROR in
action number 1
of  Step Event0
for object pauseMenu:

ini_write_string argument 3 incorrect type (0) expecting a String (YYGS)
 at gml_Script_ini_save (line 5) - ini_write_string("Player", "Room", room);

Is there something I'm doing wrong with saving the room, or do I have to go about it a different way?


Solution

  • room is a number, not a string

    ini_open("save.ini");
    
    ini_write_string("Player", "Room", room_get_name(room));
    ini_write_real("Player", "X", oPlayer.x);
    ini_write_real("Player", "Y", oPlayer.y);
    
    ini_close();
    

    For back transform you should use asset_get_index. Something like this:

    ini_open("save.ini");
    var r_name = ini_read_string("Player", "Room", "");
    global.startx = ini_read_real("Player", "X", 0);
    global.starty = ini_read_real("Player", "Y", 0);
    
    ini_close();
    
    if r_name == "" or (global.startx == 0 and global.starty == 0)
    {
        room_goto(r_level1); // first room
    }
    else
    {
        var r = asset_get_index(r_name);
        if r != -1 and asset_get_type(r_name) == asset_room
            room_goto(r);
    }
    

    (I don't know, need you these extra checks really or not)