INI Save/Load [GameMaker: Studio]

You’ve made a game and it’s going well, but you need to add a save feature so that the player can continue with their adventure – ini functions will do the trick.

ini functions use an ini file to store numerical and string values which can be written to the file and read later on. Let’s see how we can save a simple thing in an ini file – let’s take the score of the game.

Saving the score

First, let’s create a save button, and when clicked, it will save the score. Add the Left Pressed event and add a code action.

For this, we’ll use the ini_open function. It will create an ini file if there isn’t one already, and open it if it’s already there.

ini_open("savedata.ini");

We’ve specified the ini file name here. As the game probably doesn’t have this ini file already, it will create it and open it. Now we’ll write the score variable to the ini file. For this, I assume that the score is saved in a global hitScore variable. You can change it accordingly.

A global variable is a variable which can be read and changed by any object. A normal variable can be used by any object too, but for that you need to put in the name of the object having the variable (like obj_player.hitScore to use a normal variable).

ini_write_real("savegame", "score", hitScore);

This function writes a numerical value to the ini file. Since our hitScore stores a numerical value, we’ll use this function and not the string variant of it.

In an ini file, there are keys which store the value, and sections which store the keys. Here savegame is the section in the ini file I will create which will store the key score which stores my hitScore value.

ini_close();

This functions saves your changes to the ini file and closes it. Now we’re done with this code.

Now lets create a new Load button and add some code in its Left Pressed event so that we can use it to load our score.

ini_open("savedata.ini");

hitScore = ini_read_real("savegame", "score", 0);

ini_close();

Okay, this is almost similar to our previous code. The first line opens the savedata.ini file we created first. In the second line, we are reading the hitScore value using ini_read_real() which we saved last and then assigning it to the variable hitScore. This will change our score in-game.

In ini_read_real(), we have first specified the section to look in, then the key to look for, and then the “default” value which is the value given to the hitScore variable if no save is found.

And then, ini_close() to close the ini file.

Try it in your game! Make some score, save it, re-start the game, and load the score!

Saving player location

Suppose I have a player with the variables hitScore (stores the score), playerLevel (stores the level of the player) and playerName (stores the name in a string). Now when I save the game, I want to save these things:

  • Player’s location
  • Player’s score and level
  • Player’s name

To save the player’s location, we’ll save the room id and x/y locations so that we can load them. For score and level, we’ll use ini_write_real() as we did above, and for the name we’ll use ini_write_string() which stores a string value.

Here I assume our player’s object as obj_player. We’ll use the variables from the obj_player object. Let’s create a Save button object and add some code:

ini_open("savedata.ini");

ini_write_real("savegame", "roomID", room); //saves room id with key roomID

ini_write_real("savegame", "xPos", obj_player.x); //saves player X location

ini_write_real("savegame", "yPos", obj_player.y); //saves player Y location

ini_write_real("savegame", "score", obj_player.hitScore); //saves hitScore value

ini_write_real("savegame", "level", obj_player.playerLevel); //saves playerLevel value

ini_write_string("savegame", "name", obj_player.playerName); //saves 

ini_close(); //saves changes to ini file and closes

If you didn’t know already, text after the // is called a comment, and it has no effect on the code – it’s just a comment, plain text where you can write anything you want.

This is pretty much self-explanatory, so let’s move on and create a Load object. Here’s the code for the Load object:

var roomID;

ini_open("savedata.ini");
roomID = ini_read_real("savegame", "roomID", 0);
obj_player.x = ini_read_real("savegame", "xPos", 0);
obj_player.y = ini_read_real("savegame", "yPos", 0);
obj_player.hitScore = ini_read_real("savegame", "score", 0);

obj_player.playerLevel = ini_read_real("savegame", "level", 0);
obj_player.playerName = ini_read_string("savegame", "name", "unknown");
ini_close();
room_goto(roomID);

This is simpler than it looks. For the most part, we’re reading the values we previously saved and assigning it to variables which belong to obj_player – that’s why every variable has  obj_player. before it.

I created a variable roomID at the start and then used it to store the room ID read from the ini file. Then after closing the ini file, I used room_goto() to move to the room stored in the variable. Simple, isn’t it? 😉

Conclusion

It’s really this easy to save information about your game and load it later on. As stated in the GMS documentation, ini files work on almost all platforms. Using ini functions you can store any value so if used nicely, this is a pretty useful thing.

Thanks for reading, have a great day and happy game-making!

P.S. Any suggestions/questions? Comment below and I’ll get back to you ASAP!

Advertisements

3 thoughts on “INI Save/Load [GameMaker: Studio]

  1. Hi i have a question if i was to update my game would players loose their ini saved progress once games updated or would the old ini saved file still have all their saved progress so far and they can still continue on with new updates .

    Thanks in advance

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s