Porting NSUserDefaults values

Porting NSUserDefaults values
0.0 0


Hi guys,

I am in the process of porting my iOS app from cocos2d-iphone and objective-c to cocos2d-x but I have hit one problem. A lot of my save structure is based around nsmutablearrays for storing the state of a level, ie unlocked/locked/skipped but there in cocos2d-x’s CCUserDefault class there isnt anything set up for storing arrays to the device.

I’m guessing plenty of people have come across this issue, I wondered what was the best plan of attack for storing this information?



I usually use the string key to identity arrays. For example

#define COLUMN_PLAYER "player"
#define COLUMN_SCORE "score"
for (int i = 0; i < 10; i++)
    char[32] szKey = {0};
    sprintf(szKey, "userdata-row%d-%s\0", i, COLUMN_PLAYER);
    CCUserDefault::sharedUserDefault()->setStringForKey(szKey, szPlayerName);
    sprintf(szKey, "userdata-row%d-%s\0", i, COLUMN_SCORE);
    CCUserDefault::sharedUserDefault()->setIntegerForKey(szKey, nScore);

No matter you are storing multi-dimension array or map, just combine the dimensions identifier into the key string.