Saving player info



I would like to save player´s info like points, total time player, etc.

What´s the best way to do this in cocos2dx, in a portable way, meaning that I can use the method in iOS, Android, etc?

CCUserDefault would be a good choice?




i think yes or you could use your own SQLite database :slight_smile:


CCUserDefault is already portable.


Using a binary file would be a good approach?

struct Config{
int score;
int soundOn;


Then, I could use fopen/fread/fwrite/fclose to update the file contents, using CCFileUtils::fullPathForFilename to get the full file name with path.

Do you see any problem in this approach?


I’ve read somewhere here that fwrite doesn’t work on Android. I’m not sure, would be best to try it.



So, how do you save game info in Android, Blackberry, iOS, etc?


I’ve read that fwrite works on ios/android here: and I’m using same approach on ios and it works. Didn’t check on android.


I’m using android the the code below, no issues.
I first write out the length of the array as the first value and then loop through my array writing out each value
LevelProgress is my struct holding the level info.

    std::string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + "\\levelProgress.f";

    FILE* f = fopen(path.c_str(), "wb");

    int size = _levelProgress.size();

    fwrite(&size, sizeof(int), 1, f);

    for(unsigned int i = 0; i<_levelProgress.size(); i++)
        fwrite(&_levelProgress[i], sizeof(LevelProgress), 1, f);



I didn´t know CCFileUtils::getWriteablePath, it returns a safe place to store the file?

Great tip,
Thanks a lot!


Is there are reason you are not able to use CCUserDefault?


CCFileUtils::getWriteablePath returns the correct path on each platform where you can safely read and write files to / from.

I could have used CCUserDefault (which is where I’m saving my settings like volume).
I wanted to store my info in a proper structure but CCUserDefault only supports simple types.

If cocos2dx supported proper object xml serialisation I would have used that but the way I’m doing it was quicker to implement.


I need to store a binary array of data, thats why I´m not using CCUserDefault.

If CCUserDefault could store binary values, it would be nice!

Maybe translating the binary data to a string…

Thanks for the tips, guys!


The code I posted should do exactly what you need.


Yes, great!

That´s what I need.

Thank you.