• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

HTML5 [SOLVED] Save/Load lags hard for HTML5? or am I doing something wrong?

Hi, I have a save/load system and it works 100% perfect on the Windows export, but when I try it on HTML5, it will either lag hard or freeze entirely. I don't know what is causing this. Would it be better to use cookies for HTML5 instead? Or ini?

I have 3 save files, and when you go into the save room or load room, all 3 files get opened in order for them to be read and drawn data from them on the screen, and of course I close them when I am done using the files.

Code:
///scr_save_game()




//Create the save data structure
var save_data = ds_map_create();

//Player
save_data[? "money"] = global.money;
save_data[? "day"] = global.day;



////////////////////////////////////////////


var save_string = json_encode(save_data);
ds_map_destroy(save_data);
save_string = base64_encode(save_string);

var file = file_text_open_write(working_directory + argument0);
file_text_write_string(file, save_string);
file_text_close(file);

Code:
///scr_load_game()

var file = file_text_open_read(working_directory + argument0);
var save_string = file_text_read_string(file);

file_text_close(file);

save_string = base64_decode(save_string);
var save_data = json_decode(save_string);



if (!is_undefined(ds_map_find_value(save_data, "day"))) {
    global.day = save_data[? "day"];
}

if (!is_undefined(ds_map_find_value(save_data, "money"))) {
    global.money = save_data[? "money"];
}
Draw Event
Code:
if (file_exists(file)) {
 
    draw_text(x-50, y-425, "Day: " + string(save_data[? "day"]));
 
    draw_text(x-50, y-375, "Money: $" + string(save_data[? "money"]));
 
 
}
Left Pressed Event
Code:
scr_save_game(file);



if (file_exists("Save1.txt") and fileNumber == 1) {
 
    if (global.file1Open == true) {
 
        file_text_close(fileOpen);
 
    }

    fileOpen = file_text_open_read(working_directory + "Save1.txt");
    save_string = file_text_read_string(fileOpen);

    save_string = base64_decode(save_string);
    save_data = json_decode(save_string);
 
    global.file1Open = true;
 
}

I will be saving data structures, and it says in the documentation that I should be using text files to save and load.

Can somebody point me in the right direction at least?
 
Last edited:
Try adding an empty base version
Try adding an empty base version of the save file as an Included File in your project ("Save1.txt")
of the save file as an Included File in your project ("Save1.txt")
I tried this and it didn't seem to make a difference for me. It only lags for me when you first go into the save or load room, and then after that every time you go into those rooms it would be instant. Is this normal? (For Windows, it is always instant)

I only have these 2 code blocks running when you enter the room:

Instance Creation Code
Code:
fileNumber = 1;



fileOpen = file_text_open_read(working_directory + "Save" + string(fileNumber) + ".txt");
save_string = file_text_read_string(fileOpen);

save_string = base64_decode(save_string);
save_data = json_decode(save_string);

Draw Event
Code:
draw_self();

draw_set_color(c_white);
draw_set_halign(fa_center);
draw_set_valign(fa_center);
draw_text(x+sprite_width/2, y+sprite_height/2, "Save File " + string(fileNumber));
draw_set_halign(fa_left);
draw_set_valign(fa_left);

draw_set_alpha(0.8);
draw_set_color(c_black);
draw_rectangle(x-100, y-20, x+350, y-500, false);
draw_set_alpha(1);
draw_set_color(c_white);

if (save_data[? "day"] != -1) {
 
    draw_text(x-50, y-425, "Day " + string(save_data[? "day"]));
 
    draw_text(x-50, y-375, "Money $" + string(save_data[? "money"]));
 
 
} else {
 
    draw_text(x+10, y-300, "Empty File");
 
}
For me, it lags only for 1 second for when you enter either room for the very first time. (If you enter either room and then reload the HTML5 game and try to go into either room again, the rooms would be instantly loaded instead of a 1 second lag delay.)

For my first game that had this save/load system, some of my players say the game freezes when they try to save it, or that it lags a lot. (First game had A LOT of variables being saved/loaded)

It doesn't lag for me to save or load on my 2nd game, it just lags for me when I enter either room for the first time.
 
Last edited:

chmod777

Member
It's because file_text_open_read() works like that:
- It first checks if a local storage entry with the specified argument exists. If it does, then the value of this local storage value will be returned.
- If it does not, it will try to load it as if it was a file hosted on your server. And as it's loaded synchronously, it may introduce delays in some browsers.

So I think this is why the game lags the very first time (after that, the local storage entry exists, so there is no more problem).
Also, ini_open() has the same "issue". Maybe a simple and quick fix would be to use ds_map_secure_save / _load as they only deal with local storage.
 

Ricardo

Member
All these functions you are using are not async (so they will hold the execution). However, If I remember well, the major culprit in your code is file_exists. It seems to look first into the server (pausing the game in the meanwhile) and then it looks into the local storage. This behavior is highly undesirable for most cases IMO.
What I do in my projects is create an external function in JS that looks into the local storage only, and I avoid file_exists when I know is not necessary to look into the server. If you're doing a save/load system, all the content is written into the Local Storage, so there is zero need to look into the server using file_exists.

Example of the JS functions:
Code:
function file_exist_localstorage (path, content) {
    if (localStorage.getItem(path) == null)
        return 0;
    else
        return 1;
}
 
It's because file_text_open_read() works like that:
- It first checks if a local storage entry with the specified argument exists. If it does, then the value of this local storage value will be returned.
- If it does not, it will try to load it as if it was a file hosted on your server. And as it's loaded synchronously, it may introduce delays in some browsers.

So I think this is why the game lags the very first time (after that, the local storage entry exists, so there is no more problem).
Also, ini_open() has the same "issue". Maybe a simple and quick fix would be to use ds_map_secure_save / _load as they only deal with local storage.
Seems like it is working instantly now on HTML5 ever since I changed it to ds_map_secure_save / _load !!!! (Or at least, it is working instantly for me.)

Thank you so much!!! :)

I had no idea file_text_open_read() didn't work too well with HTML5. This is very useful to know!
 
Last edited:
Top