Pay very close attention to both the section, key, and values as typos can easily cause data to load in incorrectly or even crash your game.
This code creates and opens an INI file. It writes the player's x, y, hp, and coins values to that INI file, and then closes it.
If there are more variables you wish to save, add them here. They don’t have to be from the player, they can be from any instance.
We should now load data back from this file when the room starts.
Load Data from File
Go to the obj_player object, and add the Other > Room Start event.
When a room starts, and the checkpoint.ini file exists, it means the player was defeated and should be placed at a checkpoint.
That’s what this event will handle. Add this code:
if (file_exists("checkpoint.ini"))
{
ini_open("checkpoint.ini");
x = ini_read_real("player", "x", x);
y = ini_read_real("player", "y", y);
hp = ini_read_real("player", "hp", hp);
coins = ini_read_real("player", "coins", coins);
ini_close();
}
If the file exists, it loads the x, y, hp and coins values, and applies each value to the correct variable. It then closes the file.
If you saved some additional variables, load them here, and apply them to the correct instances.
Results
Run the game, touch a checkpoint and then lose.
You'll respawn at the last checkpoint you touched, with the same amount of health and coins you had when you touched the checkpoint.
This is great, but we need to take care of one more thing.
The INI file is saved to the disk, which means it can continue to exist even after the level ends or the game is closed. The file would serve no purpose past that point, and would only exist to create bugs.
We should delete the file if a level ends, or the game is closed. And, to be safe, if we detect the INI file when the game starts up, we should delete it.
Deleting the Checkpoint File
Open the obj_persistent_manager object, found under Objects -> Managers.
Add the Game End event, found under Other.
Add the following code:
if (file_exists("checkpoint.ini"))
{
file_delete("checkpoint.ini");
}
This code will delete the checkpoint file whenever the game is closed, if it exists.
obj_persistent_manager is persistent, which means it isn't destroyed when the room changes. It’s created in the first room (rm_menu) and lasts throughout the whole game.
So an instance of this object always exists and this code will always run when the game ends.
We also want this to happen on Game Start, so right-click on your Game End event and select “Duplicate Event”.
From the list, choose Other > Game Start.
Room End
Next, open the Room End event of obj_game_manager, and add the following code.
var _player_defeated = instance_exists(obj_player_defeated);
if (!_player_defeated)
{
if (file_exists("checkpoint.ini"))
{
file_delete("checkpoint.ini");
}
}
This code will delete the checkpoint file at room end, but only if the player was not defeated.
If obj_player_defeated doesn’t exist, it means the player is still alive. We’re doing this so if the player was actually defeated, the file should not be deleted, as the player needs to respawn at the last checkpoint and needs the data from the file.
However, if the player is alive when a room ends, it means they were not defeated, and either won the level, or exited through the pause menu.
In that case, we want to delete the file, which this event handles.
Summary
This tutorial covered a simple checkpoint example. However, INI files can also be used to create save files.
Simply save your player’s information when the game ends, and load it back when they start the game again.