Today we will deal with basic saving and loading functions. It will provide some quick examples that can be implemented in almost any game. If you have any questions, comments, or suggestions, feel free to post them in the forum or email me at the address shown at the bottom of the page.
Initializing FAT for saving and loading is fairly simple. Call all of you basic initializations for PAlib, then call fatInitDefault() to initialize libfat (which is a devkitPro library).
Our code so far is:
#include <PA9.h> int main(){ PA_Init(); PA_InitVBL(); fatInitDefault(); while(true){ PA_WaitForVBL(); } }
First we will go over how to write save data to a file.
First you must create a structure for the save data. In it, you will put everything that you will be saving to the file. The structure for saving data in this example will use bool and int, but others should work too. Here is the structure we will be using for this example:
typedef struct { bool character; int level; } save_struct; save_struct save_data;
Now we will actually write the structure to a file use the structure above. To do this, we will need to have defined our structure, and initialized FAT. Here is a sample code for writing save_data to a file:
#include <PA9.h> // Save structure typedef struct { bool character; int level; }save_struct; // Save data save_struct save_data; int main(){ PA_Init(); PA_InitVBL(); // FAT initialization fatInitDefault(); // Set the save_data values save_data.character = true; save_data.level = 2; // WB trunacates write and create. This will be used to create and write to the save file. FILE* save_file = fopen("save_file.sav", "wb"); // Write save_data to save_file don't forget the & for save_data fwrite(&save_data, 1, sizeof(save_data), save_file); // Close the file fclose(save_file); while(true){ PA_WaitForVBL(); } }
First we set the values here:
save_data.character = true; save_data.level = 2;
Then we open the file that we want to save the data to.
// WB trunacates write and create. This will be used to create and write to the save file FILE* save_file = fopen("save_file.sav", "wb");
After that, we write the data to the file with fwrite:
// Write save_data to save_file don't forget the & for save_data fwrite(&save_data, 1, sizeof(save_data), save_file);
Then finally, we close the file.
// Close the file fclose(save_file);
And that should work.
Reading save data is pretty much the same as writing it, but instead we will load values into the structure. We will use the same info from Writing the save data in this too. Please note, that the values for the structure must be identical to the ones that you are loading.
In order to read save data from a file, you will need to change fwrite and wb. Change fwrite to fread and wb to rb to read the data instead. Pretty simple, right? That will replace the structures previous values with the ones that are loaded.
In conclusion there are a few things that you should know about this tutorial:
As stated at the top, if you have any questions, comments, or concerns, contact me at:
— GEMISIS 05/02/2009 21:54
Next → Day 22 - Splash Screens