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

SOLVED Trouble loading high score names

YoSniper

Member
Hello everyone. I'm putting a project together using Game Maker Studio 1.4.

I'm trying to store high scores locally in a binary file, and I've been having some trouble getting the bytes to translate properly.

So far, I've managed to get it to the point where the volume options and high score values read and write just fine, but for some reason, the names associated with the high scores are not coming through.

The object responsible for managing this is called World. Below are the save and load scripts I presently have. For some reason the strings won't load into World.high_score_names. Each name is supposed to be 16 characters long, and by default, each name is a string of 16 space characters.

save_data()
Code:
///save_data()

/*
Writes the file at SAVE_FILE_NAME, Saves high scores,
names, and volume settings.
*/

var write_file = file_bin_open(SAVE_FILE_NAME, 1);

//Bytes 1 and 2 represent the audio volume options
file_bin_write_byte(write_file, World.bgm_volume);
file_bin_write_byte(write_file, World.sfx_volume);

//For the next 5 cycles, the first 4 bytes represent the high score,
//and the following 16 bytes represent the ORD value of the character
var ii, jj, total_value, name_str;
for(ii = 0; ii < 5; ii += 1) {
    total_value = World.high_score_values[ii];
    for(jj = 0; jj < 4; jj += 1) {
        val = total_value mod 100;
        file_bin_write_byte(write_file, val);
        total_value = total_value div 100;
    }
    name_str = World.high_score_names[ii];
    for(jj = 0; jj < 16; jj += 1) {
        val = ord(string_copy(name_str, jj + 1, 1));
        file_bin_write_byte(write_file, val);
    }
}

//Close the file
file_bin_close(write_file);
load_data()
Code:
///load_data()

/*
Reads the file at SAVE_FILE_NAME, and parses the bytes to read saved high scores,
names, and volume settings.
*/

var read_file = file_bin_open(SAVE_FILE_NAME, 0);

//Bytes 1 and 2 represent the audio volume options
var val = file_bin_read_byte(read_file);
World.bgm_volume = val;
audio_emitter_gain(World.bgm_emitter, World.bgm_volume / 100);
val = file_bin_read_byte(read_file);
World.sfx_volume = val;
audio_emitter_gain(World.sfx_emitter, World.sfx_volume / 100);

//For the next 5 cycles, the first 4 bytes represent the high score,
//and the following 16 bytes represent the ORD value of the character
var ii, jj, total_value, name_str;
for(ii = 0; ii < 5; ii += 1) {
    total_value = 0;
    for(jj = 0; jj < 4; jj += 1) {
        val = file_bin_read_byte(read_file);
        val *= power(100, jj);
        total_value += val;
    }
    World.high_score_values[ii] = total_value;
    name_str = "";
    for(jj = 0; jj < 16; jj += 1) {
        val = file_bin_read_byte(read_file);
        val = chr(val);
        name_str += chr(val);
    }
    World.high_score_names[ii] = name_str;
}

//Close the file
file_bin_close(read_file);
Any help as to why the strings won't load is appreciated.

EDIT: I found the problem. Apparently the chr function doesn't choke when you feed it a string instead of an integer. I accidentally called it twice inside of the load_data script, so I was getting back bogus string values.
 
Last edited:
Top