Save and Load your Game - coldrockgames/gml-raptor GitHub Wiki
Before we dive deeper into the details of the save and load process, I want to answer the most important question first:
How can I save and load my game, and how can I encrypt it?
All functions of the savegame
system are prefixed with savegame_
. This leads to some funny names like savegame_save_game
, but I still prefer a good and readable prefix, so intellisense can show me all functions for a specific topic. GameMaker does the same. Type string
or room
or instance
to see all functions belonging to that.
Let's start with saving your game.
It is as easy as calling this function!
savegame_save_game_async(filename, FILE_CRYPT_KEY)
.on_finished(function() {
ilog("Game saved successfully");
});
It is recommended, that you use the FILE_CRYPT_KEY
macro as the second parameter. It can be found in the Game_Configuration
script.
This is the mirror function to saving the game. It will load a previously saved game and restore all instances at their locations, depth, frame, etc.
savegame_load_game_async(filename, FILE_CRYPT_KEY, <optional_room_transition>)
.on_finished(function() {
ilog("Game loaded successfully");
});
You must provide the exact same crypt key that was used when saving the game.
Important
Loading a game does not restart or reset the room! But it will perform a room_goto
(or launch the specified transition), if the current room is not the same as the room stored in the savegame!
Loading works like this:
- All
Saveable
objects are destroyed. - The file is loaded, and all objects contained in it are created.
- All object references are restored.
- The User Event 15 (
onGameLoaded
) is invoked on each of the created objects. - The
.on_finished
is invoked.
Saving and loading the game has many aspects, and it is simply impossible to get every imaginable situation into a single method. That's the reason why the User Events have been implemented -- you always have a hook where you can put your required extra code on a per instance basis.
I highly recommend that you read the other parts of the Savegame
documentation as well to avoid surprises!
Continue with The Saveable Object.
To understand the .on_finished
callback and other possible callbacks, look at the raptor File Access (Sync/Async) page, as the savegame system is based on these.