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

Savefile System Not Working

Pedro Y

Member
Hi people, new to the GMS file system here, and I met a problem with saving my game.

Basically, I have 3 files I need to save, dnahs rnahs and proteinhs. I have a high score object that was created at the start of the game, and the create event looks like this:
GML:
globalvar dnahs;
globalvar rnahs;
globalvar proteinhs;

dnahs = 0;
rnahs = 0;
proteinhs = 0;
#macro DNAHS "dna.sav"
#macro RNAHS "rna.sav"
#macro PROTEINHS "protein.sav"

if (file_exists(DNAHS))
{
    var file0 = file_text_open_read(DNAHS);
    var target0 = file_text_read_real(file0);
    file_text_close(file0);
    dnahs = target0;
}
if (file_exists(RNAHS))
{
    var file1 = file_text_open_read(RNAHS);
    var target1 = file_text_read_real(file1);
    file_text_close(file1);
    rnahs = target1;
}
if (file_exists(PROTEINHS))
{
    var file2 = file_text_open_read(PROTEINHS);
    var target2 = file_text_read_real(file2);
    file_text_close(file2);
    dnahs = target2;
}
The game end event looks like this:
GML:
var file0 = file_text_open_write(DNAHS);
file_text_write_real(file0,dnahs);
file_text_close(file0);

var file1 = file_text_open_write(RNAHS);
file_text_write_real(file1,rnahs);
file_text_close(file1);

var file2 = file_text_open_write(PROTEINHS);
file_text_write_real(file2,proteinhs);
file_text_close(file2);
I will not include all of my codes here because they are too long, but the global variables worked nicely without a problem when I was playing through my game. However, when I close the game and try to play it again, the global variables just all return to 0. Can someone please help?
 

Soso

Member
IMPORTANT! The globalvar declaration is deprecated and only supported for legacy purposes. You should always use the global. identifier to mark global variables.

GML:
//remove this
globalvar dnahs;
globalvar rnahs;
globalvar proteinhs;

//remove this
dnahs = 0;
rnahs = 0;
proteinhs = 0;

//do this
global.dnahs = 0;
global.rnahs = 0;
global.proteinhs = 0;
You're probably gonna have to go thru your code and add the global. to your vars
 
globalvar being deprecated wouldn't impact the behavior here, as no functionality has actually been changed - YYG is just making it clear that "globalvar" may be removed in the future and games being made now shouldn't use it. I primarily use globalvar and have never had an issue. However, using globalvar opens up the possibility of programming errors stemming from mixing up global, instance, and local variables, so you'll want to make sure your problems don't stem from that.

I just ran your code in a test project (adding code to change the value of dnahs, rnahs, and proteinhs right before saving) and it worked as expected. Just to be clear, you're changing values of dnahs, rnahs, and proteinhs elsewhere, right? Otherwise I'd expect the global variables to be set to 0 when loading the file - that's what you wrote into it, after all.

Assuming that's not the problem, start by moving the save code from the game end event to something else, so you can debug it. GMS's debugger won't stop for breakpoints on code in the game_end event. You want to use the debugger to make sure that the values you want to save are set to something other than 0 when saving, and that you're not getting errors when trying to save (try var testval = file_text_write_real(file2,proteinhs); and make sure testval is 1). If that's working, you'll also want to step through during loading, and anywhere else in the game that changes the value of dnahs etc, to make sure those values are what you expect them to be.
 

Pedro Y

Member
Thanks RhyminGarFunkle and Soso! I solved the problem by using another method rather than my current file system, which uses buffers. If you stumble across this post and have the same problem, check out the video below by Shawn Spalding:
This method is much easier as well as more suitable for different platforms.
 
Top