Skip to main content

Command Palette

Search for a command to run...

Saving and Loading game state to the file

Updated
•1 min read•View as Markdown

We need to create a global macro in the first object we believe is created (in my case the menu object) to save the name of the file.

#macro SAVEFILE "save_test.sav"

Then we can use this macro in our code to refer to our save file.

Now, whenever we create a new Room in our player object, we can save the name of the room to the file and load it later


if (file_exists(SAVEFILE)) {
    file_delete(SAVEFILE); 
}

//create new save
var file = file_text_open_write(SAVEFILE);
file_text_write_string(file, room_get_name(room));
file_text_close(file);

We can access the file following this code:

var file = file_text_open_read(SAVEFILE);
var target = file_text_read_string(file);
var room_id = asset_get_index(target);
file_text_close(file);
go_to_room(room_id)

Saving multiple lines to the file.

If you want to save multiple lines, you need to actually write new lines to the file. And just read them in the exact same order. You can do that like this:

file_text_write_string(file, "string 1");
file_text_writeln(file);
file_text_write_real(file, variable);
file_text_writeln(file);
file_text_close(file);

More from this blog

G

GameMaker Journey

14 posts

Here, I will share the code snippets about different basic functionalities in GameMaker