Simple data storage-Userdefault for cocos2d-x

Source: Internet
Author: User

* *********************************** Please specify the source: bytes ********************************************




I haven't updated the cocos2d-x for a long time, it's too busy, Wood has time AH = ..




1. Basic concepts

This article introduces the Userdefault class for simple data storage in the API:


It is to store some simple data, such as opening and disabling the sound, enabling and disabling the sound, storing the highest score and the amount of gold coins.




2. Get

These are based on some basic types, such as int, float, double, bool, string, and binary,

There are corresponding set and get functions for their access, such:

Find Userdefault. h

Bool getBoolForKey (const char * pKey); <span style = "white-space: pre"> </span> int getIntegerForKey (const char * pKey ); float getFloatForKey (const char * pKey); double getDoubleForKey (const char * pKey); std: string getStringForKey (const char * pKey); Data getDataForKey (const char * pKey ); <span style = "white-space: pre"> </span> // binary

The pKey in the parameter is the name you set when you set it, which is similar to an address. Through this address, you can find what is stored here. In addition, there are two parameters included, for example:

Bool getBoolForKey (const char * pKey, bool defaultValue );

The others are the same as this. There are two parameters, both of which are defaultValue, but the types are different,

It means that if you cannot find the value through pKey and there is no value in it, you can let it return the defaultValue value and set it to the defaultValue value.

In fact, if there is no defaultValue, the same is true, but the value is set and 0 is returned:

<Span style = "font-family: Comic Sans MS; font-size: 14px;"> int UserDefault: getIntegerForKey (const char * pKey) {return getIntegerForKey (pKey, 0);} int UserDefault: getIntegerForKey (const char * pKey, int defaultValue) {const char * value = nullptr; tinyxml2: XMLElement * rootNode; tinyxml2: XMLDocument * doc; tinyxml2: XMLElement * node; node = getXMLNodeForKey (pKey, & rootNode, & doc); // find the nodeif (node & node-> FirstChild ()) {value = (const char *) (node-> FirstChild ()-> Value ();} int ret = defaultValue; if (value) {ret = atoi (value);} if (doc) {delete doc;} return ret ;}</span>

The above two get function settings for the int in the UserDefault class, you can see that there is no defaultValue parameter, the execution is to set the defaultValue to 0 function.




3. Set

For the set, it is very easy, just set the value.

Void setBoolForKey (const char * pKey, bool value); void setIntegerForKey (const char * pKey, int value); void setFloatForKey (const char * pKey, float value ); void setDoubleForKey (const char * pKey, double value); void setStringForKey (const char * pKey, const std: string & value); void setDataForKey (const char * pKey, const Data & value );




4. About flush

Then there is the flush function,

I don't know much about this function, and there are different opinions on the Internet,

The API saves the content to an XML file,

The definition in. cpp is as follows:

Void UserDefault: flush (){}

That's right. And, in my game, there is no use of the flush function.




5. How to use

The corresponding functions of the class are all described, and the next step is the usage problem,

First, check whether the file exists and initialize it if it does not exist:

If (! LoadBooleanFromXML ("_ IS_EXISTED") {initUserData (); SaveBooleanToXML ("_ IS_EXISTED", true );}

The format must be:

CCUserDefault: sharedUserDefault ()-> setBoolForKey (m_Sound, true); CCUserDefault: sharedUserDefault ()-> getBoolForKey (m_Sound );

There must be a long string of things before, so macro definitions will be set to simplify it in actual use, for example:

# Define GetBool CCUserDefault: sharedUserDefault ()-> getBoolForKey # define SetBool CCUserDefault: sharedUserDefault ()-> setBoolForKey




* *********************************** Please specify the source: bytes ********************************************

Simple data storage-Userdefault for cocos2d-x

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.