3 posts were merged into an existing topic: Saving user data on EEPROM & SD - development discussion
Armageddon is the first game ever to use the PokittoCookie highscore / data save system
I have updated the bin, and added this to EmBitz examples. I will update the mbed version also.
Example of how the savegame works in Armageddon:
1.Add “PokittoCookie.h” to the includes
#include "PokittoCore.h"
#include "PokittoCookie.h"
2.Create your own child class of Cookie
/* This is how you do it with the PokittoCookie class !!!*/
class gdata : public Pokitto::Cookie {
public:
uint32_t highscores[NUM_HIGHSCORES];
char names[NUM_HIGHSCORES][NAME_SIZE+1];
int check_number;
gdata() {
}
};
3.Make an instance of your class that you will use to handle the savegame data
gdata gamedata;
4.Replace direct EEPROM reads writes with a logic that uses the Cookie instead, this is the loadHighscores that runs when the game start
void loadHighscores(){
uint32_t defdata[] = {10000,7000,5000,4000,2000};
char defnames[NUM_HIGHSCORES][NAME_SIZE+1] = {"Crack Shot", "Defender", "Gunner", "We Tried", "No Hope"};
// Initialize the cookie
gamedata.begin("ArmageDD",gamedata);
uint32_t allscores=0;
for (int i=0;i<NUM_HIGHSCORES;i++) allscores += gamedata.highscores[i];
if (gamedata.check_number != 12345) {
//checknumber indicates that no highscores saved yet, so put in "defaults"
for (int i=0;i<NUM_HIGHSCORES;i++) gamedata.highscores[i]=defdata[i];
for (int i=0;i<NUM_HIGHSCORES;i++) strcpy(gamedata.names[i],defnames[i]);
gamedata.check_number = 12345; // put the checking number
gamedata.saveCookie();//save the default values
}
/*
for( uint8_t entry = 0; entry < NUM_HIGHSCORES; entry++ ){
for( uint8_t offset = 0; offset < ENTRY_SIZE; offset++ ){
if( offset < NAME_SIZE ){
names[entry][offset] = EEPROM.read( ENTRY_SIZE * entry + offset );
}else{
uint8_t* addr = (uint8_t*) &highscores[entry];
addr+=offset-NAME_SIZE;
*addr = EEPROM.read( ENTRY_SIZE * entry + offset );
}
}
}
*/
}
5.Handle variables inside the Cookie just like you would handle any other variables. When you are done, just call the saveCookie() function!!
void saveHighscore(uint32_t score, char* who){
uint8_t found = 0;
uint32_t tmp_score = 0;
char tmp_name[10];
for( uint8_t entry = 0; entry < NUM_HIGHSCORES; entry++ ){
if( score > gamedata.highscores[entry] ){
found = 1;
}
if( found ){
tmp_score = gamedata.highscores[entry];
strcpy(tmp_name,gamedata.names[entry]);
gamedata.highscores[entry] = score;
strcpy(gamedata.names[entry], who);
score = tmp_score;
strcpy(who, tmp_name);
}
}
gamedata.saveCookie();
/*Writing to EEPROM directly is deprecated and not necessary
for( uint8_t entry = 0; entry < NUM_HIGHSCORES; entry++ ){
for( uint8_t offset = 0; offset < ENTRY_SIZE; offset++ ){
if( offset < NAME_SIZE ){
EEPROM.write( ENTRY_SIZE * entry + offset, names[entry][offset] );
}else{
uint8_t* addr = (uint8_t*) &highscores[entry];
addr+=offset-NAME_SIZE;
EEPROM.write( ENTRY_SIZE * entry + offset, *addr );
}
}
}
*/
}
PokittoLib updated in mbed online compiler, this can now be compiled & runs from there as well
Awesome! I’m glad you chose Armageddon to demonstrate PokittoCookie. I never got around to addressing the issues with the previous highscore code, so this should be a good improvement.
I unfortunately have not been in the same place as my Pokitto, and I won’t be for over another month, which is part of why I haven’t been posting. I’m excited to try out this newer version on my pokitto when I get the chance though!