Legacy GM Issues with loading health and score

Hi again! It's been a hot minute since I posted here. I'm having issues with loading health and score. For instance, if I go into a room, then a script I have runs to save the game. I have it save the current room, health and score. I tried making all the objects managing these persistent, then removing them from the other rooms and keeping them in one room so that the values carry over from room to room. Although, when I try to load the game, rooms that did not have the objects managing these so the health and score would not appear.

They also use global values (both being in the player). Should I try moving those variables from the player to the objects that manage the health and score? Or should I try a different approach?

Here's some of the code I use, just in case there's something wrong that I can't figure out.
GML:
///Load file script
if (file_exists("save.sav"))
{
    ini_open("save.sav");
    var LoadedRoom = ini_read_real("save1","room",room0);
    global.playerhp = ini_read_real("save1","playerhealth",100);
    global.playerscore = ini_read_real("save1","playerscore",0);
    ini_close();
    room_goto(LoadedRoom);
}
else
{
//Do nothing
}
Code:
///Save file script
if (file_exists("save.sav")) file_delete("save.sav");
ini_open("save.sav");
var SavedRoom = room;
ini_write_real("save1","room",SavedRoom);
ini_write_real("save1","playerhealth",global.playerhp);
ini_write_real("save1","playerscore",global.playerscore);
ini_close();
If there is more code you need to have a look at to see what's wrong, I'll gladly provide some.
Thanks in advance!
 

FrostyCat

Redemption Seeker
Then you re-create the health and score managers as part of loading. Since you've made them persistent, they'll come with you to the loaded room.
GML:
if (file_exists("save.sav"))
{
    ini_open("save.sav");
    var LoadedRoom = ini_read_real("save1","room",room0);
    global.playerhp = ini_read_real("save1","playerhealth",100);
    global.playerscore = ini_read_real("save1","playerscore",0);
    ini_close();
    instance_create(0, 0, objHealthManager);
    instance_create(0, 48, objScoreManager);
    room_goto(LoadedRoom);
}
Also remove all health and score managers from every room, and instead make the "Start Game" button or whatever you use to start a session create them the same way.

I also strongly recommend that you save room names instead of room IDs for future-proofing purposes. IDs can shift around if you rearrange the resource tree later.
 
Then you re-create the health and score managers as part of loading. Since you've made them persistent, they'll come with you to the loaded room.
GML:
if (file_exists("save.sav"))
{
    ini_open("save.sav");
    var LoadedRoom = ini_read_real("save1","room",room0);
    global.playerhp = ini_read_real("save1","playerhealth",100);
    global.playerscore = ini_read_real("save1","playerscore",0);
    ini_close();
    instance_create(0, 0, objHealthManager);
    instance_create(0, 48, objScoreManager);
    room_goto(LoadedRoom);
}
Also remove all health and score managers from every room, and instead make the "Start Game" button or whatever you use to start a session create them the same way.

I also strongly recommend that you save room names instead of room IDs for future-proofing purposes. IDs can shift around if you rearrange the resource tree later.
Sorry for the late response, but I'll try this out. Will mark the thread as solved if it works. Thanks for the help!
 
Top