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
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
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
This opens an ini file and writes data to it, overwriting, and sections and keys, if already present. The file is then closed.
Open the INI files, read or write data, and then close it when done; leaving an INI file open can lead to issues.
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
- 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.
- B)
Set a counter that saves how many times the spacebar has been pressed. Set it to save/load as needed.
Advanced Project
- 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
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.