COCOS2DX: Data storage-userdefault

Source: Internet
Author: User
Tags lua

Some of the more simple data in the game can be saved using Userdefault, which is stored in an XML file format.

The Userdefault definition is as follows:

/**
* Userdefault acts as a tiny database. You can save and get base type values by it.
* For example, Setboolforkey ("played", true) would add a bool value true into the database.
* It key is "played". You can get the value of the key by Getboolforkey ("played").
*
* It supports the following base types:
* bool, int, float, double, string
*/
Class Cc_dll Userdefault
{
Public
Get Value methods


/**
@brief Get bool Value by key, if the key doesn ' t exist, a default value would return.
You can set the default value, or it is false.
* @js NA
*/
BOOL Getboolforkey (const char* PKey);
/**
* @js NA
*/
BOOL Getboolforkey (const char* PKey, bool defaultvalue);
/**
@brief Get integer value by key, if the key doesn ' t exist, a default value would return.
You can set the default value, or it is 0.
* @js NA
*/
int Getintegerforkey (const char* PKey);
/**
* @js NA
*/
int Getintegerforkey (const char* pKey, int defaultvalue);
/**
@brief Get float Value by key, if the key doesn ' t exist, a default value would return.
You can set the default value, or it is 0.0f.
* @js NA
*/
Float Getfloatforkey (const char* PKey);
/**
* @js NA
*/
Float Getfloatforkey (const char* PKey, float defaultvalue);
/**
@brief Get Double value by key, if the key doesn ' t exist, a default value would return.
You can set the default value, or it is 0.0.
* @js NA
*/
Double Getdoubleforkey (const char* PKey);
/**
* @js NA
*/
Double Getdoubleforkey (const char* PKey, double defaultvalue);
/**
@brief Get String value by key, if the key doesn ' t exist, a default value would return.
You can set the default value, or it is "".
* @js NA
*/
std::string getstringforkey (const char* PKey);
/**
* @js NA
*/
std::string getstringforkey (const char* PKey, const std::string & defaultvalue);
/**
@brief Get binary data value by key, if the key doesn ' t exist, a default value would return.
You can set the default value, or it is null.
* @js NA
* @lua NA
*/
Data Getdataforkey (const char* PKey);
/**
* @js NA
* @lua NA
*/
Data Getdataforkey (const char* PKey, const data& defaultvalue);


Set Value methods


/**
@brief Set bool Value by key.
* @js NA
*/
void Setboolforkey (const char* PKey, bool value);
/**
@brief Set integer value by key.
* @js NA
*/
void Setintegerforkey (const char* pKey, int value);
/**
@brief Set float value by key.
* @js NA
*/
void Setfloatforkey (const char* PKey, float value);
/**
@brief Set Double value by key.
* @js NA
*/
void Setdoubleforkey (const char* PKey, double value);
/**
@brief Set string value by key.
* @js NA
*/
void Setstringforkey (const char* PKey, const std::string & value);
/**
@brief Set binary data value by key.
* @js NA
* @lua NA
*/
void Setdataforkey (const char* PKey, const data& value);
/**
@brief Save content to XML file
* @js NA
*/
void Flush ();


/** returns the Singleton
* @js NA
* @lua NA
*/
Static userdefault* getinstance ();
/**
* @js NA
*/
static void Destroyinstance ();


/** deprecated. Use Getinstace () instead
* @js NA
* @lua NA
*/
Cc_deprecated_attribute static userdefault* Shareduserdefault ();
/**
* @js NA
*/
Cc_deprecated_attribute static void Purgeshareduserdefault ();
/**
* @js NA
*/
Const static std::string& Getxmlfilepath ();
/**
* @js NA
*/
static bool Isxmlfileexist ();


Private
Userdefault ();
~userdefault ();

static bool Createxmlfile ();
static void Initxmlfilepath ();

Static userdefault* _userdefault;
Static std::string _filepath;
static bool _isfilepathinitialized;
};

Use the following:

Userdefault::getinstance ()->setstringforkey ("Key", "value"); Add a string of data to the specified key, userdefault::getinstance () if it is the first call, it will automatically generate a userdefault.xml file that holds all the data in the Userdefault. The data in the Userdefault is stored in a key-value way.

Userdefault::getinstance ()->getstringforkey ("Key", "Default value");//reads the data of the specified key, or returns the specified default value if the data does not exist Value

Userdefault::isxmlfileexist ();//To determine if a userdefault.xml file already exists

If the code is too long to call, you can use a macro, such as

#define Savestringtoxml userdefault::getinstance ()->setstringforkey

#define Loadstringfromxml userdefault::getinstance ()->getstringforkey

...

#define Savebooleantoxml userdefault::getinstance ()->setboolforkey

#define Loadbooleanfromxml userdefault::getinstance ()->getboolforkey sometimes need to decide whether or not to run/generate some data for the first time, you can use a Boolean value to record, as

if (Loadbooleanfromxml ("First_time", True))

{

Make the required first load operation

Savebooleantoxml ("First_time", false);

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.