gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Saving game data

#171018 - gauauu - Wed Oct 28, 2009 7:27 pm

Split from here

sverx wrote:
gauauu wrote:
For NDS, you need to write to the filesystem using libfat, which comes with libnds. Once you do, it's mostly as easy as writing files to any filesystem. If you need more help, start a new thread about it and we'll discuss there.

For GBA, I just now answered genecyst's question about it here:
http://forum.gbadev.org/viewtopic.php?p=171011#171011


Thanks! Isn't there anything on the NDS like what's on the GBA? I mean, is there a way to implement a save using no filesystems? How do the commercial titles do, then???


Commercial titles use different types of cartridges than homebrew. I'm not sure how they do it, but it's definitely different. The problem is that our carts are generally adapters to SD cards with a filesystem. A loader loads the actual rom into the DS memory when you start. So there's really no place/way of storing data other than using the file system to read/write the SD card.

It would be possible (and not too difficult) to make a library that abstracts some of that away, but it would still be happening under the covers, and as it's actually pretty easy to read/write using the file system, it doesn't seem to be very necessary.

I have example code of doing so if you are interested.

#171021 - vuurrobin - Wed Oct 28, 2009 8:01 pm

don't official ds cards have some kind of rom chip to save stuff on? isn't that what the eeprom is for?


flashcards don't have this memory, because then games would overwrite eachother save data. instead, if somebody tries to save data to this memory, they stream the data to the filesytem.


however, this shouldn't be used by homebrewgames because you rely on a workaround of the loader, and that may not always work (some loaders don't support it, others encript the file, ect). not to mention that you try to write to memory that you know that doesn't exists.


ds homebrew games should just use the filesystem in one way or another (you can use efs to save the data inside the nds file, but that still needs the filesystem)
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#171051 - sverx - Thu Oct 29, 2009 11:17 am

Thanks both, it's much clearer now :)

#171080 - Azenris - Fri Oct 30, 2009 7:40 pm

Hi I was actually just starting to have some save data, is that the idea then to just have a file on the card for your game like "MySave.bin" etc. I just wrote
Code:
// =================================================================================
// SaveGame:
bool CGame::ACCOUNT_INFO::SaveGame(void)
{
   if (g_pGame->FileSystemOK())
   {
      FILE *pFile = fopen("RedTempleSaveData.bin", "wb");            // open the file
      
      if (pFile != NULL)
      {
         fseek(pFile, 0, SEEK_SET);                           // seek the the file start
         fwrite((void*)this, sizeof(ACCOUNT_INFO), 1, pFile);      // read all the data into the account
         fclose(pFile);                                    // close the file

         return true;
      }
      else
      {
         ASSERT(0, "Failed To Save")
         return false;                                    // an error occured
      }
   }

   return false;
}

// =================================================================================
// LoadGame:
bool CGame::ACCOUNT_INFO::LoadGame(void)
{
   if (g_pGame->FileSystemOK())
   {
      FILE *pFile = fopen("RedTempleSaveData.bin", "rb");            // open the file
      
      if (pFile != NULL)
      {
         fseek(pFile, 0, SEEK_SET);                           // seek the the file start
         fread((void*)this, sizeof(ACCOUNT_INFO), 1, pFile);         // read all the data into the account
         fclose(pFile);                                    // close the file

         return true;
      }
      else
      {
         return false;                                    // no save file
      }
   }

   return false;
}


Seems to work so far, hope im not doing sommet that will damage someones card !
_________________
My Homebrew Games

#171082 - gauauu - Fri Oct 30, 2009 9:19 pm

Also, one thing that bit me: don't name your save file (yourRomName).sav (ie I first used anguna.sav for my save file)

Certain cards use this file internally, and it'll cause all sorts of problems.

#171085 - vuurrobin - Fri Oct 30, 2009 9:55 pm

and, to avoid cluttering up the root of the memory card, use DATA/yourgame/yourgame.sav to save your savedata, or something like it.
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#171089 - a128 - Sat Oct 31, 2009 9:17 am

compress youre save data....i.e. using LZ77
_________________
"Hey! It compiles! Ship it!"

#171091 - gauauu - Sat Oct 31, 2009 3:19 pm

a128 wrote:
compress youre save data....i.e. using LZ77


Here, I wouldn't necessarily always agree. If you're only saving a tiny bit of data (which should often be the case), then it's not worth the effort.

Anguna would save:
-your location
-your stats (health (and max), armor, attack, money) (5 ints)
-Your inventory (an int for 1 each of about 6 items)
-Game state flags (About 150 of them. I used a full int even though it was wasteful)
-Flags for each enemy to determine if you've fought it or not (about 30)
-A checksum

So that's around 200 ints. Most ds writable cards are huge (500 megs to a few gigs?). Compression just seems like a waste of time.

#171092 - a128 - Sat Oct 31, 2009 3:49 pm

gauauu wrote:


So that's around 200 ints. Most ds writable cards are huge (500 megs to a few gigs?). Compression just seems like a waste of time.


This quote perfectly shows ....with huge memory no one cares to optimize things. this is sad

writing less data ...is faster ....good for your life of the card too!
.and reading less data is faster too.

I post a LZ77 compressor/decompressor is you like...because I use this to save /load my data on the NDS
_________________
"Hey! It compiles! Ship it!"


Last edited by a128 on Sat Oct 31, 2009 3:55 pm; edited 1 time in total

#171093 - Dwedit - Sat Oct 31, 2009 3:52 pm

It depends on what you are using to save. If you have an SD card, go ahead and save 8 gigs of crap.

But if you are using a real cartridge, your save storage is far more limited. In the Nintendo 64 era, most games used 512 byte EEPROM saves, though 2KB EEPROM, 32KB SRAM, and 128KB Flash was also available.
On the GBA, saves were either 128 byte EEPROM, 4K EEPROM, 32 KB SRAM, 64 KB FLASH, or occasionally 128KB FLASH.
On the DS, save sizes range from 512 byte EEPROM to 512KB Flash.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#171094 - a128 - Sat Oct 31, 2009 3:58 pm

Dwedit wrote:
It depends on what you are using to save. If you have an SD card, go ahead and save 8 gigs of crap.


Thx to Dwedit who cares about save sizes.
_________________
"Hey! It compiles! Ship it!"

#171095 - vuurrobin - Sat Oct 31, 2009 5:04 pm

a128 wrote:
with huge memory no one cares to optimize things.


IMO, there is a difference with optimising your savedata and compressing your savedata. if you use ints just to store a boolean value, then sure go ahead and optimise it so it stores boolean values in a byte or a bit.

I wouldn't start thinking about compressing savadate until it is at least a few kb. anything below that is just not necessary. remember that (de)compressing also takes time and that you may not save that much memory by compressing.


a128 wrote:
I post a LZ77 compressor/decompressor is you like...because I use this to save /load my data on the NDS


doesn't the DS have some bios functions for LZ77 compression.


Dwedit wrote:
If you have an SD card, go ahead and save 8 gigs of crap.


and be prepared to get alot of emails because users can't store both your game and another game on there SD card at the same time, or that they need a bigger SD card just to play your game.

if users have a big SD card, they useally want to store alot of games, not a single game with a freaking big savedata (but I doubt that any homebrew game will have savedata bigger than an mb)
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#171096 - gauauu - Sat Oct 31, 2009 5:07 pm

Dwedit wrote:

But if you are using a real cartridge, your save storage is far more limited.


Which is why I prefaced this by mentioned writable homebrew nds carts. If you have tighter constraints, then it might make sense to compress.

Quote:

This quote perfectly shows ....with huge memory no one cares to optimize things. this is sad

writing less data ...is faster ....good for your life of the card too!
.and reading less data is faster too.


I prefer finishing things to unnecessary optimization ;-) (Not to say you wouldn't finish, but I wouldn't if I optimized things that didn't need it)

#171097 - a128 - Sat Oct 31, 2009 7:18 pm

vuurrobin wrote:


doesn't the DS have some bios functions for LZ77 compression.


only for decompressing ....or did i missed somethink?!
_________________
"Hey! It compiles! Ship it!"

#171100 - vuurrobin - Sun Nov 01, 2009 12:18 am

no I think you're right. sorry, my mistake <insertEmbarrisingSmilieHereAsSoonAsTheForumsAllowSmilies>
_________________
my blog:
http://vuurrobin.100webcustomers.com/