© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_18

18. INI Files

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 
If you have a game that people will play for a few minutes at a time, you will want to save certain data so that they can continue the next time they play. Things you will want to save may include
  • Player’s name, health, lives, and score.

  • Any previous highscore.

  • The total distance they have moved.

  • How many bullets have been fired.

  • What keys they have collected.

  • Which treasure chests have been opened.

  • Which enemies have been defeated.

INI File Contents

An easy way to save and load data is with INI files. INI files consist of two main elements:

[sections] and keys

A simple ini file would look like this:
[player]
highscore=1000
name="Ben"
Note

On most exports, it is not required to include an INI as an included file. You can set this as a text file with the required section, keys and values.

Loading Data

To load data from this file you can use
ini_open("savedata.ini");
global.highscore=ini_read_real("player","highscore", 0);
global.player_name=ini_read_string("player","name","None");
ini_close();

This code will open the INI file and set two global values that can be used in game, using the data that is stored in it.

If no INI file exists (or a section/key is not present), for example, it is the first time the game has been played, then the values would have the default values set to them, in this case global.highscore would be set to 0 and global.player_name as “None.” The default value is after the last, . If the INI file exists, then the saved value will be set.

Saving Data

Writing a value to an INI is just as easy:
ini_open("savedata.ini");
ini_write_real("levels_completed","level",global.levels);
ini_write_string("player","name",global.player_name);
ini_close();

This opens an ini file and writes data to it, overwriting, and sections and keys, if already present. The file is then closed.

Note

Open the INI files, read or write data, and then close it when done; leaving an INI file open can lead to issues.

After saving these values, the INI file may look like this:
[levels_completed]
level=14
[player]
"name"="Richard"

ini_read_real() and ini_write_real() are used with reals and integers.

ini_read_string() and ini_write_string() are used with strings.

Basic Projects

  1. A)

    Set a splash room to load any saved data. If data is present, set the player to the saved location upon game room starting. Make a player that can be moved using arrow keys. Upon pressing x, save the players location and restart the game.

     
  2. B)

    Set a counter that saves how many times the spacebar has been pressed. Set it to save/load as needed.

     

Advanced Project

  1. C)

    Make a system that saves five players’ info: name, age, favorite food. Upon game start, offer to display saved data (if present) or create new data.

     

Useful Functions

You can check if a key or section exists:
ini_section_exists();
ini_key_exists();
for example, with an open ini file
if !ini_section_exists("Level_1")
{
      ini_write_real("Level_1", "Complete", true);
}
You can delete the whole ini file with:
if file_exists("game_data.ini")
{
    file_delete("game_data.ini ");
}

Summary

You now understand the structure of an INI file and how to write and read data. You should now be able to make a basic save system to save a players game progress.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.131