GML [1.4.9999] SOLVED having an issue with save/load

woods

Member
following along this short quick tutorial to save/load my game


according to awesome F1 help button, my default save directory is
%localappdata%\<Game Name> directory

the save button works.. as i can navigate to the folder and see the changes made to the ini file.
my problem is with the load button.. not sure if it is not reading the file or the player stats are being overwritten when the room is loaded..


obj_save_button
left_pressed event
GML:
/// L click to save

ini_open(working_directory + 'savedata.ini');
ini_write_real('savegame', 'roomID', room); //saves room id with key roomID
ini_write_real('savegame', 'xPos', obj_player.x); //saves player X location
ini_write_real('savegame', 'yPos', obj_player.y); //saves player Y location
ini_write_real('savegame', 'my_level', obj_player.my_level);
ini_write_real('savegame', 'my_exp', obj_player.my_exp);
ini_write_real('savegame', 'my_target_exp', obj_player.my_target_exp);
ini_write_real('savegame', 'my_health', obj_player.my_health);
ini_write_real('savegame', 'my_max_health', obj_player.my_max_health);
ini_write_real('savegame', 'my_gold', obj_player.my_gold);
ini_write_string('savegame', 'state', obj_player.state);
ini_close();

obj_load_button
left_pressed event
GML:
/// L click to load

var roomID;
ini_open(working_directory + 'savedata.ini');
roomID = ini_read_real('savegame', 'roomID', 0);
obj_player.x = ini_read_real('savegame', 'xPos', 0);
obj_player.y = ini_read_real('savegame', 'yPos', 0);
obj_player.my_level = ini_read_real('savegame','my_level',1);
obj_player.my_exp = ini_read_real('savegame','my_exp',0);
obj_player.my_target_exp = ini_read_real('savegame','my_target_exp',0);
obj_player.my_health = ini_read_real('savegame','my_health',0);
obj_player.my_max_health = ini_read_real('savegame','my_max_health',0);
obj_player.my_gold = ini_read_real('savegame','my_gold',0);
obj_player.state = ini_read_string('savegame','state','idle');
ini_close();
room_goto(roomID);
if it has any bearing..
play room is not persistent
i have an instance of obj_player placed in the room thru the editor



a little assistance would be appreciated
 
Last edited:

chamaeleon

Member
GML:
ini_open(working_directory + 'savedata.ini');
GML:
ini_open(working_directory + 'savedata.ini');
It should not be required to use working_directory. Using ini file should work just fine without any directory prefix at all, unless you want to store ini files in subdirectories.
 

woods

Member
i was fiddling with
ini_open('savedata.ini');

i added the working_directory.. forgot to take that bit out.

same results either way
 

chamaeleon

Member
A single object for test purposes.
Create event
GML:
global.foo = 1;
global.bar = "one";
Mouse left click event
GML:
global.foo += 1;
ini_open("savedata.ini");
ini_write_real("game", "foo", global.foo);
ini_write_string("game", "bar", string(current_time/1000));
ini_close();
Mouse right click event
GML:
ini_open("savedata.ini");
global.foo = ini_read_real("game", "foo", 100);
global.bar = ini_read_string("game", "bar", "empty");
ini_close();
Draw event
GML:
draw_text(16, 16, "foo  = " + string(global.foo));
draw_text(16, 32, "bar  = " + global.bar);
draw_text(16, 48, "time = " + string(current_time/1000));
draw_self();
With an exported executable, clicking the left button a couple of times to update foo, and save foo and bar. Exit program and restart, right-click mouse, results in foo being loaded with the value that was stored and bar with the time value at the time saving was performed.
 
You're storing your save data in instance variables and then switch rooms.
This means all your instance variables get reset, unless obj_player is a persistent object, which I guess is not the case.

Use global variables instead

Also: switch to " " instead of ' ' or you'll get in trouble when using ' in a string. " is the default anyway in GMS 2
 

woods

Member
what i think is happening is my room is overriding the loaded data from the ini

in the obj_load_button
....
ini_close();
room_goto(roomID);
 

Nidoking

Member
Is the obj_player persistent, or is the obj_player not persistent? You must answer this simple question if you want a meaningful answer from the community.
 
Top