CCUserDefault::sharedUserDefault()->**** Not working.

CCUserDefault::sharedUserDefault()->**** Not working.
0.0 0


I am trying to save user data(Highscores) using set******ForKey functions. But they dont seem to work properly.
this is the source code below. When I debug the program. The value for key “string” and “b” is always “” and 0. (“” because a string is returned and 0 is the default value for int).
Is there something wrong with CCUserDefault class? If so, what are the alternatives? Or is it just that I am doing something wrong?
Running on Windows. Win32 Application.

CCUserDefault::sharedUserDefault()->setStringForKey(“string”, “value1”);
int hurah= CCUserDefault::sharedUserDefault()->getIntegerForKey(“b”);
std::string rev = CCUserDefault::sharedUserDefault()->getStringForKey(“string”);

I have run the CCUserDefault test, And they too do not work alright. (Value returned is always 0").


which version of cocos2d-x are you using?


cocos2d-2.1rc0-x-2.1.3. It worked fine yesterday. I didn’t change anything but now it has stopped working.
I read that it saves data in an xml file. Searched for the xml file but couldn’t find that either.


The same code runs fine on Android.


what i saw that you are enclosing both the arguments in double quotes when you are using getIntegerforkey or getStringforkey.
first argument should be a variable const char *.Here is an example

const char * HIGH_SCORE="high_score";
CCUserDefault *def=CCUserDefault::sharedUserDefault();
def->setIntegerForKey(HIGH_SCORE, 2000);

To retrieve values from the key,suppose we want to retrieve the high score and display on a label

int high_score=def->getIntegerForKey(HIGH_SCORE);
CCLOG("high score %d",high_score);

It will work fine on IOS,android and windows too
If you want more details you can visit my blog

Happy Coding