Save and Load your Game - Grisgram/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.

savegame_save_game

Let's start with saving your game.

It is as easy as calling this function!

savegame_save_game(filename, cryptkey);

If you want the file to be encrypted, provide any (long, complicated, random characters) string as the cryptkey parameter, or simply omit that parameter to have the file saved as plain text in json format.


Tip

If you are not familiar with Configurations in GameMaker, take a look at the manual here: GameMaker Configurations.
It's a powerful tool that can provide dynamically set #macro values based on the active configuration.

So, if you want your savegame to be encrypted in a release compile but always be plain text while you develop and debug it, you could do a setup like this:

In the active default configuration define a macro as an empty string like this:
#macro SAVEGAME_CRYPT_KEY ""

Now create a configuration called "release" and add this line right beneath of your definition of the macro:
#macro release:SAVEGAME_CRYPT_KEY "fjdif(98/F8>f8u0q03874=(ZSOFIHA?§§%.,aoufpaÖKPPOJM"

Having done that, you can simply save your game with

savegame_save_game(SAVEGAME_FILE_NAME, SAVEGAME_CRYPT_KEY);

Depending on your active configuration (default or release) when you compile, the file will be encrypted or not.


savegame_load_game

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(SAVEGAME_FILE_NAME, SAVEGAME_CRYPT_KEY);

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, 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 savegame_load_game function returns and your code continues in the line after.

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.

⚠️ **GitHub.com Fallback** ⚠️