[IOS-cocos2d-X Game Development Five] Game storage of cocos2dx built-in ccuserdefault class details;

Source: Internet
Author: User

This siteArticleBoth Li huaming himi Original, reprinted must be clearly noted: (Author: Sina Weibo:@ Li huaming himi)Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/iphone-cocos2dx/653.html ☞Click to subscribe☜The latest developments in this blog! Notify you of the latest blog in time!


Share

 

 

This article will share with you the storage in cocos2dx, which also introduces some details that are prone to mistakes;

The built-in storage class ccuserdefault is provided in cocos2dx. Of course, here himi emphasizes that if your data volume is large, it is recommended that you use SQL storage,Do not use platform-related APIs in cocos2dx for development. For example, when xcode uses cocos2dx for game development, IOS controls/components are accidentally used in the project, so when we port it to Android and other platforms, it will certainly be very difficult. It is estimated that even normal operation will not be possible, because other platforms may not have just these IOS controls, even if there are also certainly different underlying implementations!In other words, all Shenma functions are implemented using the cocos2dx API. Try to move them closer to X, so I also use the ccuserdefault provided by X; at least cocos2dx must support cross-platform platforms;

Let's get down to the point. Let's take a rough look at the APIs of this class:

123456789101112131415161718192021222324252627282930 Public member functions ~ Ccuserdefault () Bool Getboolforkey ( Const Char * Pkey, Bool Defaultvalue = False ) Get Bool Value by key, If The key doesn' t exist, Default Value will Return . Int Getintegerforkey ( Const Char * Pkey, Int Defaultvalue = 0) Get integer value by key, If The key doesn' t exist, Default Value will Return . Float Getfloatforkey ( Const Char * Pkey, Float Defaultvalue = 0.0f) Get Float Value by key, If The key doesn' t exist, Default Value will Return . Double Getdoubleforkey ( Const Char * Pkey, Double Defaultvalue = 0.0) Get Double Value by key, If The key doesn' t exist, Default Value will Return . STD: String getstringforkey ( Const Char * Pkey, Const STD: string & defaultvalue = "" ) Get string value by key, If The key doesn' t exist, Default Value will Return . Void Setboolforkey ( Const Char * Pkey, Bool Value) Set Bool Value by key. Void Setintegerforkey ( Const Char * Pkey, Int Value) Set integer value by key. Void Setfloatforkey ( Const Char * Pkey, Float Value) Set Float Value by key. Void Setdoubleforkey ( Const Char * Pkey, Double Value) Set Double Value by key. Void Setstringforkey ( Const Char * Pkey, Const STD: string & value) Set string value by key. Void Flush () Save content to XML file. Static public member functions Static Ccuserdefault * shareduserdefault () Static Void Purgeshareduserdefault () Static Const STD: string & getxmlfilepath ()

The usage and functions of ccuserdefault, hash table structure, key-value, and key index value can be clearly seen from the above;

The provided storage is of some basic types, such as bool, Int, String, double, and float. The method is easy to understand: Use set for storage and get for get!

In the static method, we can see that the ccuserdefault class sets aside a shareduserdefault interface for developers to use. After a rough introduction, let's write a few paragraphs below.CodeVerify:

12345678 // Here we simply store the data records Ccuserdefault: shareduserdefault ()-> setstringforkey ( "Key" , "Himi" ); Ccuserdefault: shareduserdefault ()-> flush (); // Here you must submit and write data. Otherwise, the data will not be recorded in the XML file. You will not be able to obtain the value next time you start the game. // Define a string here to verify our storage String STR = "Wahaha" ; // Retrieve the himi we just stored and assign it to STR for verification; STR = ccuserdefault: shareduserdefault ()-> getstringforkey ( "Key" ); Cclog ( "Print STR =: % s" , Str. c_str ());

Note that there is a flush () function in ccuserdefault, which is used to write data into the XML file. That is to say, when you use some setxx functions, remember to submit them (call the flush function)

OK. The result entered in the console is as follows:

12345678910111213 Cocos2d: cocos2d: cocos2d-1.0.1-x-0.12.0 Cocos2d: cocos2d: gl_vendor: Imagination Technologies Cocos2d: cocos2d: gl_renderer: powervr SGX 543 Cocos2d: cocos2d: gl_version: OpenGL ES-CM 1.1 IMGSGX543-63.14.2 Cocos2d: cocos2d: gl_max_texture_size: 4096 Cocos2d: cocos2d: gl_max_modelview_stack_depth: 16 Cocos2d: cocos2d: Gl supports pvrtc: Yes Cocos2d: cocos2d: Gl supports bgra8888 textures: No Cocos2d: cocos2d: Gl supports npot textures: Yes Cocos2d: cocos2d: Gl supports discard_framebuffer: Yes Cocos2d: cocos2d: compiled with npot support: No Cocos2d: cocos2d: compiled with VBO support in textureatlas: No Cocos2d: Print STR =: himi

The last statement verifies that our storage is okay. Now we verify that there is an XML file. First, stop the currently running project and delete the code to replace the following code:

1 Cclog ("Print STR =: % s", Ccuserdefault: shareduserdefault ()-> getstringforkey ("Key"). C_str ());

Then run the project again. The following figure is displayed on the console:

12345678910111213 Cocos2d: cocos2d: cocos2d-1.0.1-x-0.12.0 Cocos2d: cocos2d: gl_vendor: Imagination Technologies Cocos2d: cocos2d: gl_renderer: powervr SGX 543 Cocos2d: cocos2d: gl_version: OpenGL ES-CM 1.1 IMGSGX543-63.14.2 Cocos2d: cocos2d: gl_max_texture_size: 4096 Cocos2d: cocos2d: gl_max_modelview_stack_depth: 16 Cocos2d: cocos2d: Gl supports pvrtc: Yes Cocos2d: cocos2d: Gl supports bgra8888 textures: No Cocos2d: cocos2d: Gl supports npot textures: Yes Cocos2d: cocos2d: Gl supports discard_framebuffer: Yes Cocos2d: cocos2d: compiled with npot support: No Cocos2d: cocos2d: compiled with VBO support in textureatlas: No Cocos2d: Print STR =: himi

Through the key-> "key" just now, we can get the "himi" string normally. OK, no problem in monitoring;

In general, we need a method to determine whether the current project has an XML file that stores data. In this case, the default source code of cocos2dx includes this method, however, this function is not provided to developers because it is private. The source code of this function is shown in:

In this case, himi has customized a function to check whether XML data already exists. (Note: Many kids shoes should say that. Why don't I directly modify the source code to make it public ?! In fact, this is also true for himi. However, if you use a new cocos2dx version later, or the cocos2dx of a colleague machine does not modify the source code so much, it will produce errors. In other words, since it is easy to write a judgment method, why bother to write it, isn't it? Haha !)

1234567891011121314151617 . H file: Bool ishavesavefile ( ) ; . Cpp file: / / Whether the current project has stored XML files Bool helloworld : : Ishavesavefile ( ) { If ( ! Ccuserdefault : : Shareduserdefault ( ) - > Getboolforkey ( "Ishavesavefilexml" ) ) { Ccuserdefault : : Shareduserdefault ( ) - > Setboolforkey ( "Ishavesavefilexml" , True ) ; Ccuserdefault : : Shareduserdefault ( ) - > Flush ( ) ; / / Submit / / Cclog ( "The storage file does not exist. The first time you started loading the game" ) ; Return False ; } Else { / / Cclog ( "The storage file already exists" ) ; Return True ; } }

Note: When the XML used to store data does not exist, you will create the first time you store the data. The documents in the path under your app will be created by default, as shown in:

 

Here, himi emphasizes one point! Pay attention to the parameters of the setxx function, such as the following function:

Setstringforkey (const char * pkey, const STD: string & value)

The first parameter is of the const char * type, not a string !!!! (Himi is a waste of time and tragedy for this reason .)

Himi stored the following code at the time, causing an error:

1 Ccuserdefault: shareduserdefault ()-> setstringforkey (""+ 823, skey );

The error is as follows: (the stored key is changed to the path .... Data is encrypted by himi)

Alas, I'm depressed. Here, I make a mistake and hope that my shoes will not fan up this error. Previously, himi was always trying to find a function for ITOA, but I couldn't find it! (C ++ should have an integer to a string), but it does not exist in cocos2dx. In the end, himi uses the source code to implement itoa in the cocos2dx engine and finds it as follows:

The ccuserdefault provided by cocos2dx is not encrypted, but in plain text and. XML format. Therefore, himi will write an article on base64 encryption for your reference;

Download this article source code: unzip savedataforcocos2dx.zip ": http://vdisk.weibo.com/s/hq2Ys

Related Article

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.