CCUSerdefautl and file writing?

CCUSerdefautl and file writing?
0.0 0


most probably 2 stupîd questions from a newcomer:

when using CC userDefault do I need totake care of file writing / loading ? where do I need to call it so that my data is available throughout my game? (for example in a MenuSetting scene to modify them)

How can make a global variable available through my game (like saving some game state) From below code itseems I need to declare in the Appdelegate.m but how then can I access it in my class instances??

//////////////////////////* code I’d like to reuse?

/ global variables
int gLevel;
int gScore;
in my app delegate:
// if this is the first time ever running
// this app we need some default defaults

  • (void)initialize{
    NSDictionary *appDefaults = [NSDictionary
    dictionaryWithObjects:[NSArray arrayWithObjects:
    [NSNumber numberWithInt:0],
    [NSNumber numberWithInt:1],
    forKeys:[NSArray arrayWithObjects:
    "Score", // starts at score of 0“Level”, // starts at level 1
    [[NSUserDefaults standardUserDefaults]

// we are quitting
// save current score and level to the defaults

  • (void) applicationWillTerminate:(UIApplication*)application
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber
    numberWithInt:gScore] forKey:"Score"]; [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:gLevel] forKey:“Level”];


// we are starting up
// get the score and level
// that we saved when we last quit
// or if its the first time ever running this game
// it will load the default defaults

  • (void) applicationDidFinishLaunching:(UIApplication*)application
    gScore=[[NSUserDefaults standardUserDefaults]
    integerForKey:"Score"]; gLevel=[[NSUserDefaults standardUserDefaults] integerForKey:“Level”];



  1. CCUserDefault help you to figure out where’s the writable folder on each platform, so you don’t need to care about where it writes to & reads from.
  2. For global usage, my favorite approach is to write a singleton class, and include the class header in other cpp files, call MySettings::getInstance()->writeSomething(…) when I need. You can refer to static SimpleAudioEngine* SimpleAudioEngine::sharedEngine() function for a sample.


thanks. what ’s where I have a problem understanding at which stage that singleton class gets instanced? I need to create a an instance in the Appdelegate oninit and then release it onexit?

I will look up simpleaudioengine for the syntax, but I must confess it’s hard to follow e.g. where the shared director and sharedEngine gets created and why he is accessible throughout the life of the app…
thanks for the great support on my 4 other questions :wink:


found with google… it’s created the first time the getinstance is called but some precaution is required to … next question is then why shared director and shared engine are not called via getinstance?


There are may ways to do it.

  1. Global instance of a class
    The instance is defined in the global scope, it is instnced when the programe starts.

    // xxx.cpp
    MyClass sharedClass.

    // implementation of the class

  2. A static variable of a method

    SimpleAudioEngine* SimpleAudioEngine::sharedEngine()
    static SimpleAudioEngine s_SharedEngine;
    return &s_SharedEngine;

s_SharedEngine is initialized when the programe starts.
3. Dynamic malloc after invoking some function

ClassA *sharedPtr = NULL;
ClassA* ClassA::func(void)
    if (! sharedPtr)
        sharedPtr = new ClassA;

    return sharedPtr 

Instance is initialized after ClassA::func() invoked.


1 - excellent, thanks.
a bit like here :
(2 page down in the middle) but the code appearsto be in a .h not .cpp ?

It may be worth in the documentation to mention all the singletons: sharedDirector, sharedEngine, sharedUserDefault (any other) and to mention that cocos2dx already make them available…for new users it appears like “magic”…

2- since CCUserDefault::sharedUserDefault belongs to cocos2dx, then I can’t init it in Appdelegate i can only access it in my first scene I create?
3-Do you know in Xcode wheter the tiny database is kept between several runs meaning whether I can test if a data was properly written for the next launch of the application? or I need to test directly with the iphone?

  1. The codes are in .cpp.
  2. I think the word “shared” indicates that it is a singleton.
  3. You don’t need to init CCUserDefault, you just call CCUserDefault::sharedUserDefault to save/read the value.
  4. Sorry, I am not familiar with database of iOS, so I can not help you.


thank you indeed. I need to run a couple of simulation I gues to answer 4 :slight_smile: