Using PokittoDisk for Saving and Loading
Games that reset highscore every time you restart your Pokitto are good and all, but what if you want a persistent highscore on your game? The PokittoDisk library can do this and much more by allowing the program to interact with files on the SD card.
Files
First of all, you’ll need a file to save to. I recommend making a .txt file as they are easy to edit. If you’re using hardware to run your game, put your file on the SD card in the same folder as your Pokitto BIN (please correct me if that isn’t the correct place, as I haven’t actually tried using PD on HW yet). If you’re running your game on the simulator, navigate to /Pokitto/POKITTO_SIM/bin/Debug/
and put your file there.
Next, we need to fill the file with dummy data. For each variable you would like to store, enter:
- 1 character for a
char
or abool
- 2 characters for a
short
, anint
, or auint16_t
- 4 characters for a
long
, auint32_t
, or afloat
- 8 characters for a
long long
or adouble
These characters can be whatever you would like.
For this example, I will be using a char
for my highscore, and my file will be hiscore.txt
containing 0
.
The Setup
For this, I’ll assume that I have a highscore variable called char hiscore
, and that when I get a new highscore, I call the void saveHiscore()
function. I also will assume I have a function called when I open the game called char readHiscore()
.
Saving Highscore
Here’s the code for saving highscore:
void saveHiscore() {
fileOpen("hiscore.txt", FILE_MODE_READWRITE);
fileSetPosition(0);
filePutChar(hiscore);
fileClose();
}
Let’s go through all of this:
fileOpen("hiscore.txt", FILE_MODE_READWRITE);
What this line of code is doing is opening “hiscore.txt” in the read/write file mode. The read/write file mode is one of many file modes, but this particular one allows the program to both read and write to a file.
fileSetPosition(0);
This line is setting the file “cursor’s” position to the very first character. Notice how the characters start counting at 0 – this is similar to arrays!
filePutChar(hiscore);
This line is where all the magic happens. The code puts the ASCII character with the same number as the value of hiscore
into the location of the file “cursor.” This will be able to be read later.
fileClose();
This tells the program that we are done using “hiscore.txt,” so we can use other files if need be. It’s good to always close a file to prevent some problems from happening.
Reading Saved Highscores
Here’s the code for readHiscore()
:
char readHiscore() {
char tempHiscore;
fileOpen("hiscore.txt",FILE_MODE_READWRITE);
fileSetPosition(0);
tempHiscore = (char)fileGetChar();
fileClose();
return tempHiscore;
}
I won’t go through the functions I already mentioned in the last section, but here are the new ones:
char tempHiscore;
This is a declaration of a new variable. We’re creating this variable so we can return our highscore without having to forgo closing the file.
tempHiscore = (char)fileGetChar();
Here we’re setting tempHiscore
to the char found in the file at position 0. We have to cast to a char because for some reason, fileGetChar()
returns an int
.
return tempHiscore;
This is fairly self-explanatory. We’re returning the value we found in the file so we can use it for something (probably setting hiscore
to the saved highscore!).
Conclusion
We now have a working highscore saving mechanism. If we integrate this into our game appropriately, we should be able to not have all our progress deleted when we restart the Pokitto, which is obviously a good thing!