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

GameMaker Saving and Loading a Persistent object (INI)

Edit #2: I thought I had this figured out but now the player's coordinates don't change and the camera won't snap onto the player.

//Loading
Code:
CL = objCallList;

if file_exists("Save.helix")
{
    ini_open("Save.helix");
    var LoadedRoom = ini_read_real("Room","Room",Overworld0);
    
    CL.hp = ini_read_real("Player","HP",20);
    CL.maxhp = ini_read_real("Player","MAXHP",20);
    CL.xp = ini_read_real("Player","XP",0);
    CL.maxxp = ini_read_real("Player","MAXXP",10);
    CL.money = ini_read_real("Player","Cash",0);
    CL.lvl = ini_read_real("Player","Level",1);
    CL.skill = ini_read_real("Player","Skill",0);
    CL.cashgainmax = ini_read_real("Luck","CGMX",3);
    CL.cashgainmin = ini_read_real("Luck","CGMN",0);
    CL.xpgainmax = ini_read_real("Luck","XPGX",5);
    CL.xpgainmin = ini_read_real("Luck","XPGN",1);
    CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
    var PLX = x = ini_read_real("Player","PX",x)
    var PLY = y = ini_read_real("Player","PY",y)

    ini_close();
    
    if !instance_exists(plrHero0)
    {
        instance_create_layer(PLX,PLY,"Instances",plrHero0);
        instance_create_layer(PLX,PLY,"Instances",objDraw);
        instance_create_layer(PLX,PLY,"Instances",objCamera);
        with(ev_game_start)
        {
            plrHero0.x = PLX;
            plrHero0.y = PLY;
        }
        instance_activate_object(plrHero0);
        CL.hascontrol = true;
    }
    room_goto(LoadedRoom);
}
else
{
    show_message("There is no available save data!");
}

Original Post:

Hello everyone, I have been stuck on this topic for a while. I have saved all the variable and the players location and room. When I try and load the game it crashes or there isn't an instance of the player. Any help would be wonderful.

Code:
CL = objCallList;

ini_open("Save.helix");

var SavedRoom = room;
ini_write_real("Player","HP",CL.hp);
ini_write_real("Player","MAXHP",CL.maxhp);
ini_write_real("Player","XP",CL.xp);
ini_write_real("Player","MAXXP",CL.maxxp);
ini_write_real("Player","Cash",CL.money);
ini_write_real("Player","Level",CL.lvl);
ini_write_real("Player","Skill",CL.skill);
ini_write_real("Luck","CGMX",CL.cashgainmax);
ini_write_real("Luck","CGMN",CL.cashgainmin);
ini_write_real("Luck","XPGX",CL.xpgainmax);
ini_write_real("Luck","XPGN",CL.xpgainmin);
ini_write_real("Misc","Stat Menu",CL.statsMenu);
ini_write_real("Room","Room#",SavedRoom);

ini_close();

Code:
CL = objCallList;
if file_exists("Save.helix")
{
    ini_open("Save.helix");
    var LoadedRoom = ini_read_real("Room","Room#",-1);
  
    CL.hp = ini_read_real("Player","HP",20);
    CL.maxhp = ini_read_real("Player","MAXHP",20);
    CL.xp = ini_read_real("Player","XP",0);
    CL.maxxp = ini_read_real("Player","MAXXP",10);
    CL.money = ini_read_real("Player","Cash",0);
    CL.lvl = ini_read_real("Player","Level",1);
    CL.skill = ini_read_real("Player","Skill",0);
    CL.cashgainmax = ini_read_real("Luck","CGMX",3);
    CL.cashgainmin = ini_read_real("Luck","CGMN",0);
    CL.xpgainmax = ini_read_real("Luck","XPGX",5);
    CL.xpgainmin = ini_read_real("Luck","XPGN",1);
    CL.statsMenu = ini_read_real("Misc","Stat Menu",false);

    ini_close();
  
    room_goto(LoadedRoom);
}
else
{
    show_message("There is no available save data!");
}
 
Last edited:
Do you actually create the player prior to calling the load script?
Sorry, I just re-added the X and Y coordinates. When do I create the instance? I tried it when I revised the Load script it crashed because the var I was calling was farther down the script. Would I put the creation in a game start event?


Code:
CL = objCallList;

instance_create_layer(PLX,PLY,"Instances",plrHero0)
if file_exists("Save.helix")
{
    ini_open("Save.helix");
    var LoadedRoom = ini_read_real("Room","Room#",-1);
    
    CL.hp = ini_read_real("Player","HP",20);
    CL.maxhp = ini_read_real("Player","MAXHP",20);
    CL.xp = ini_read_real("Player","XP",0);
    CL.maxxp = ini_read_real("Player","MAXXP",10);
    CL.money = ini_read_real("Player","Cash",0);
    CL.lvl = ini_read_real("Player","Level",1);
    CL.skill = ini_read_real("Player","Skill",0);
    CL.cashgainmax = ini_read_real("Luck","CGMX",3);
    CL.cashgainmin = ini_read_real("Luck","CGMN",0);
    CL.xpgainmax = ini_read_real("Luck","XPGX",5);
    CL.xpgainmin = ini_read_real("Luck","XPGN",1);
    CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
    var PLX = plrHero0.x = ini_read_real("Player","PX",plrHero0.x)
    var PLY = plrHero0.y = ini_read_real("Player","PY",plrHero0.y)

    ini_close();
    
    room_goto(LoadedRoom);
}
else
{
    show_message("There is no available save data!");
}
 
My problem now is that when I load the room none of the objects appear. I ran a debug message to tell me if the player existed it did but wouldn't show up on screen. I revised the load script again for this.

Code:
CL = objCallList;

if file_exists("Save.helix")
{
    ini_open("Save.helix");
    var LoadedRoom = ini_read_real("Room","Room",Overworld0);
    
    CL.hp = ini_read_real("Player","HP",20);
    CL.maxhp = ini_read_real("Player","MAXHP",20);
    CL.xp = ini_read_real("Player","XP",0);
    CL.maxxp = ini_read_real("Player","MAXXP",10);
    CL.money = ini_read_real("Player","Cash",0);
    CL.lvl = ini_read_real("Player","Level",1);
    CL.skill = ini_read_real("Player","Skill",0);
    CL.cashgainmax = ini_read_real("Luck","CGMX",3);
    CL.cashgainmin = ini_read_real("Luck","CGMN",0);
    CL.xpgainmax = ini_read_real("Luck","XPGX",5);
    CL.xpgainmin = ini_read_real("Luck","XPGN",1);
    CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
    var PLX = x = ini_read_real("Player","PX",x)
    var PLY = y = ini_read_real("Player","PY",y)

    ini_close();
    
    room_goto(LoadedRoom);
    if !instance_exists(plrHero0)
    {
        instance_create_layer(PLX,PLY,"Instances",plrHero0)
    }
}
else
{
    show_message("There is no available save data!");
}
 
Room_goto() doesn't happen until the end of the script.

So the script will run, create the player then move to the next room.

If your player is not persistent, it is being created in the loading room, then destroyed when the room change happens.
 
Room_goto() doesn't happen until the end of the script.

So the script will run, create the player then move to the next room.

If your player is not persistent, it is being created in the loading room, then destroyed when the room change happens.
So where do I create the player and when?
 
I thought I had this figured out but now the player's coordinates don't change and the camera won't snap onto the player.
Code:
CL = objCallList;

if file_exists("Save.helix")
{
    ini_open("Save.helix");
    var LoadedRoom = ini_read_real("Room","Room",Overworld0);
    
    CL.hp = ini_read_real("Player","HP",20);
    CL.maxhp = ini_read_real("Player","MAXHP",20);
    CL.xp = ini_read_real("Player","XP",0);
    CL.maxxp = ini_read_real("Player","MAXXP",10);
    CL.money = ini_read_real("Player","Cash",0);
    CL.lvl = ini_read_real("Player","Level",1);
    CL.skill = ini_read_real("Player","Skill",0);
    CL.cashgainmax = ini_read_real("Luck","CGMX",3);
    CL.cashgainmin = ini_read_real("Luck","CGMN",0);
    CL.xpgainmax = ini_read_real("Luck","XPGX",5);
    CL.xpgainmin = ini_read_real("Luck","XPGN",1);
    CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
    var PLX = x = ini_read_real("Player","PX",x)
    var PLY = y = ini_read_real("Player","PY",y)

    ini_close();
    
    if !instance_exists(plrHero0)
    {
        instance_create_layer(PLX,PLY,"Instances",plrHero0);
        instance_create_layer(PLX,PLY,"Instances",objDraw);
        instance_create_layer(PLX,PLY,"Instances",objCamera);
        with(ev_game_start)
        {
            plrHero0.x = PLX;
            plrHero0.y = PLY;
        }
        instance_activate_object(plrHero0);
        CL.hascontrol = true;
    }
    room_goto(LoadedRoom);
}
else
{
    show_message("There is no available save data!");
}
I'm really confused now and not sure what to do about it. I need to fix this ASAP so I can keep working on bug fixes.
 
Top