CCUserDefault not saving value between sessions

I’m saving my highscore value using CCUserdefault like so

@
CCUserDefault::sharedUserDefault()->setIntegerForKey(“highScore”, appDelegate.score);
int highScore = CCUserDefault::sharedUserDefault()->getIntegerForKey(“highScore”);
@

This works fine while the game is running. But if I close and restart the game it goes back to 0. Is this how it works or is there something I’m missing?

I have the same problem in cocos2d-2.0-rc0a-x-2.0 before.
now,i type the next line when i saving value.

CCUserDefault::sharedUserDefault()->flush();

http://www.cocos2d-x.org/boards/6/topics/11139?r=11180#message-11180

Yeap, you should invoke flush by yourself.
I think i have to write a article to describe the design of CCUserDefault.

#1376 is created for it.

1 Like

OMG, thanks for that one. I just spent a day trying to figure out why my data wasn’t saving after upgrading it to V2!

hi guys, the CCUserDefault is not working properly in windows. does anybody has a solution for tht.

Dhivakar Subramaniam wrote:

hi guys, the CCUserDefault is not working properly in windows. does anybody has a solution for tht.

What’s the problem was? Did someone face with this problem on windows? Is that bug exists now on windows? (if this is a bug, of course).

Airex Rest wrote:

What’s the problem was? Did someone face with this problem on windows? Is that bug exists now on windows? (if this is a bug, of course).

As described above; The functionality changed, needed to call flush after saving your data to ensure it actually got written -> CCUserDefault::sharedUserDefault()->flush();

Hi. I’m experiencing this problem with WP8, even after invoking flush(). Strangely it works only in the emulator but it doesn’t work on the device I was testing: Lumia 620. I’m using cocos2dx 2.2.

After some investigation, I realized flush() is empty, and the save function has been moved to setValueForKey(), which means flush() is no longer needed and all setXXXXForKey() should save the values right away. However, my issue with the device is not yet solved. The filename parameter passed in to tinyxml2.cpp always turned to garbage, which I first believe what causing the save to fail. In desperate attempt, I tried hardcoding the filename in SaveFile() in tinyxml2, but to no avail. So I’m quite lost at what to do now.

I re-installed the app on the device, and the problem went away! Yippee! :smiley: Sometimes it helps to move away from the keyboard and refresh your mind :slight_smile:

Hello!

I have the same problem with cocos2d-x v2.2.2:

CCUserDefault::sharedUserDefault()->setStringForKey("name", m_Name); CCUserDefault::sharedUserDefault()->setIntegerForKey("level", m_Level); CCUserDefault::sharedUserDefault()->setIntegerForKey("xp", m_XP); CCUserDefault::sharedUserDefault()->setIntegerForKey("high_score", m_HighScore); cocos2d::CCUserDefault::sharedUserDefault()->flush();

on windows the code works without flush, but on ios it does not with or without flush. the userdefaults.xml does not saved. what could be the problem?