The Windy Woods template is a great starting point for your own platformer.
In this tutorial, you'll create a checkpoint object that will save the player's progress during a level.
Create a new project using the Windy Woods template. You can choose either GML Code or GML Visual:
You can use your existing Windy Woods project if you already have one.
Importing a Local Package
This tutorial will use a new sprite, spr_checkpoint. You can either create your own sprite, or download our asset package.
To import this package, click on Tools and select “Import Local Package”.
Open the asset package you just downloaded.
Choose “Add All”, and click “Import”.
You now have the checkpoint sprite needed for this tutorial.
This sprite contains two frames: an inactive checkpoint, and an activated checkpoint.
FPS is set to zero because this is not an animation. We’ll manually change the sprite’s frames depending on the checkpoint’s state.
Creating a checkpoint
Create an object and name it obj_checkpoint.
I've put this new object under Objects -> Environment -> Interactive .
Assign it the sprite spr_checkpoint (found in Sprites -> Environment -> Interactive).
Open rm_level_1:
And select the "Instance" layer.
Place some checkpoints in the room, preferably with some enemies in between, so you can test being defeated and respawning on the last checkpoint.
For example:
Activating Checkpoint
Let’s activate a checkpoint when it collides with the player.
Inside obj_checkpoint, add a collision event with obj_player.
Add the following code:
if (image_index == 1)
{
exit;
}
image_index = 1;
This checks if the image_index (which is the current frame) is 1, meaning it’s activate. In that case, it exits the event so nothing happens.
The event continues if the checkpoint is inactive. It sets the image_index to 1, activating the checkpoint, and making the second frame visible.
Run the game and see the sprite change when your player collides with a checkpoint.
When a checkpoint is activated, it should save the player’s position and stats, so they can be restored when the player is defeated.
Save Data to File
GameMaker lets you save data in many different ways! This tutorial uses INI files for saving checkpoint info.
INI files are small, lightweight files which are compatible with most platforms, and are ideal for storing small pieces of information.
Open the checkpoint’s collision event with obj_player. Here, add code to save the player’s info to an INI file:
ini_open("checkpoint.ini");
ini_write_real("player", "x", other.x);
ini_write_real("player", "y", other.y);
ini_write_real("player", "hp", other.hp);
ini_write_real("player", "coins", other.coins);
ini_close();
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.
Read more GameMaker tutorials to build your game.
Happy GameMaking!