Is it possible to save game data?

I want to design a pet-like/ tamogatchi type game. Is it possible to save game data on the Arduboy?

Yes, you can save it with Arduino’s EEPROM interface.

There’s a summary with examples of how to do different things here.

There’s 1024 bytes of EERPOM addressed from 0 to 1023, but the first 16 bytes are reserved for sound settings and other stuff.

To read a byte from an address you can do:

uint8_t value = EEPROM.read(address);

To write a byte to an adress you can do:

EERPOM.update(address, value);

(Note that there’s also an EEPROM.write, but EEPROM.update checks if the value is different beforehand so it only does a write if the new value is different.)

To do anything more than a single byte you can use get and put e.g.
get:

uint16_t value = 0;
EEPROM.get(address, value);

put:

EEPROM.put(address, value);

When you use get and put to read/store data that’s larger than one byte, you need to move the address by the size of that item, e.g.

uint16_t address = 256;

uint32_t scoreData = score;
EEPROM.put(address, scoreData);
address += sizeof(uint32_t);

Player playerData = player;
EEPROM.put(address, playerData);
address += sizeof(Player);

And vice-versa:

uint16_t address = 256;

uint32_t scoreData = 0;
EEPROM.put(address, scoreData);
address += sizeof(uint32_t);
score = scoreData;

Player playerData = {};
EEPROM.get(address, playerData);
address += sizeof(Player);
player = playerData;
1 Like