Load and save data
In this tutorial i'll show you how you very simply can load and save data. It's useful for remembering settings, hp, positions and so on.
As an easy example, we're going to make a character save its position when the game ends, and restore the position when the game starts. This will work on all devices GameMaker:Studio can export to.
Our target is the thing you see below. Click it, and you'll be able to move with the arrow keys. Refresh the site, and see that the man saved his position
As an easy example, we're going to make a character save its position when the game ends, and restore the position when the game starts. This will work on all devices GameMaker:Studio can export to.
Our target is the thing you see below. Click it, and you'll be able to move with the arrow keys. Refresh the site, and see that the man saved his position
1. Draw a beautiful sprite
.. or load it :) I don't care
2. Create an object, and make it move
Give it the sprite you just made, and call it obj_beautiful, or anything else. I'm using the name obj_beautiful in this tutorial
3. Creating the save/load object
This is the object you'll use to save and load data. Therefore, it needs two events:
1. When the game starts, we need to load obj_beautiful's position
2. When the game ends, we need to save obj_beautiful's position
1. When the game starts, we need to load obj_beautiful's position
2. When the game ends, we need to save obj_beautiful's position
4. Making obj_loadsave work
An INI file is a file where you can store two things: numbers (real) and text (string).
When the game ends, we want to:
1. Open an INI file
2. Save obj_beautiful's position to the INI
3. Close the INI file
To do this, I only need to explain one function. It's called ini_write_real(). It uses 3 arguments.
First argument = the section (the name of the place to save the data, text string)
Second argument = the key (a subname for the section, text string)
Third argument = the number to save
Of couse, ini_open() will open target INI file, and ini_close() will close target INI file.
When the game ends, we want to:
1. Open an INI file
2. Save obj_beautiful's position to the INI
3. Close the INI file
To do this, I only need to explain one function. It's called ini_write_real(). It uses 3 arguments.
First argument = the section (the name of the place to save the data, text string)
Second argument = the key (a subname for the section, text string)
Third argument = the number to save
Of couse, ini_open() will open target INI file, and ini_close() will close target INI file.
When the game starts, we want to:
1. Check if target INI file exists, stop the code if it doesn't
2. Open an INI file (if it exists)
3. Load and apply obj_beautiful's position from the INI
4. Close the INI file
Code from game start event:
1. Check if target INI file exists, stop the code if it doesn't
2. Open an INI file (if it exists)
3. Load and apply obj_beautiful's position from the INI
4. Close the INI file
Code from game start event:
5. Make a room
Create a room, and put obj_beautiful and obj_loadsave into it
Try running the game. Move the character, close the game. Then run the game again, and notice that the character now loads the position.
You've now learned how to load and save data in your game, app or anything else.
You've now learned how to load and save data in your game, app or anything else.