Quick-cocos2d-x data storage Userdefault gamestate IO

Source: Internet
Author: User
Tags readfile

See the Quick-cocos2d-x framework, found inside there is a gamestate, check the next, is the class of data storage, so a little summary of the data I used to store the way it.

There are altogether three methods:

    1. Cc. Userdefault
    2. Cc.utils.State
    3. Io

Advantages and Disadvantages

The first two are more methods to use, because the system is already well-defined. But the downside is that you can't change the file name and path at the LUA level.

So in use, according to different needs, can choose the first or third, the second is not very recommended to use, too troublesome.

General application information, such as various switches, using CC. Userdefault save, can automatically delete files with the deletion of the application.

User's account information, the use of IO storage, saved in the SD card, so that the user after reloading the application, still can enter smoothly.

The first is the use of CC. Userdefault class. This is provided by the system, the resulting file name is Userdefault.xml, file name at the LUA level is not modifiable, if you want to modify, you need to modify C + + code.

Two methods, memory and read, the main note is that different data types are called differently, such as the following example

1 LocalParamtype =2 {3Integer ="int",4String ="string",5Float ="float",6Bool ="BOOL",7Double ="Double",8 }9 Ten --Save One functionNativedata:savevaleforkey (Val, Key,type) A     if type= = Paramtype.string Then - cc. Userdefault:getinstance (): Setstringforkey (Key, Val) -     ElseIf type= = Paramtype.integer Then the cc. Userdefault:getinstance (): Setintegerforkey (Key, Val) -     ElseIf type= = Paramtype.float Then - cc. Userdefault:getinstance (): Setfloatforkey (Key, Val) -     ElseIf type= = Paramtype.double Then + cc. Userdefault:getinstance (): Setdoubleforkey (Key, Val) -     ElseIf type= = Paramtype.bool Then + cc. Userdefault:getinstance (): Setboolforkey (Key, Val) A     End at End -  - --Read - functionNativedata:getvaleforkey (Key,type, default) -     LocalVale =Nil -     if type= = Paramtype.string Then inVale =cc. Userdefault:getinstance (): Getstringforkey (key, default) -     ElseIf type= = Paramtype.integer Then toVale =cc. Userdefault:getinstance (): Getintegerforkey (key, default) +     ElseIf type= = Paramtype.float Then -Vale =cc. Userdefault:getinstance (): Getfloatforkey (key, default) the     ElseIf type= = Paramtype.double Then *Vale =cc. Userdefault:getinstance (): Getdoubleforkey (key, default) $     ElseIf type= = Paramtype.bool ThenPanax NotoginsengVale =cc. Userdefault:getinstance (): Getboolforkey (key, default) -     End the  +     returnVale A  the End

The second is the use of the Cc.utils.State class. The file location is in Framework/cc/utils/gamestate.lua. The default framework is not loaded and needs to be loaded manually if we want to use it. Typically loaded at the beginning of the MyApp.

1 require("framework.cc.utils.GameState")

After the load is complete, it needs to be initialized, Cc.utils.State. Init (Eventlistener_, Statefilename_, Secretkey_)

Called once before the scene is initialized, as in Myapp.lua's Myapp:ctor ().


Eventlistener_ is a callback function when loading or saving

Statefilename_ is the saved file name, and if left blank or non-string (string) is the default state.txt, the file is saved to Device.writablepath

Secretkey_ is the key used to validate the file, the GameState saves the data in the format {h = hash, S = s},s is the data we want to save (a table), and H is a MD5 code to validate. If Secretkey_ is left blank or is a non-string (string), the data is saved directly, just like Ccuserdefault.

After the load is complete, it is normal to use. Both the load and save methods callback the first parameter in init.

1 --This method typically only needs to be called once to load the local file into memory2 functionGameState.Load()3 End4 5 --NewValues is the new value, which is actually the object that was saved after it was loaded into memory. 6 functionGamestate.save (newValues)7 End8 9 --return full pathTen functionGamestate.getgamestatepath () One End

About Eventlistener_, you can look at http://my.oschina.net/lonewolf/blog/173063 written, very detailed.

The third is to read and write files directly using IO. This is relatively simple. The main logic is to set the Save directory to read before you need to determine if there is a directory that does not exist to create.

A Simple File tool class

Module("makefileutils",Package.seeall)LocalLFS, os, io =require"LFS", OS, iofunctionreadFile (path)LocalFile =assert(Io.open(Path,"RB"))    ifFile Then        LocalContent =File:read("*all")        Io.close(file)returncontentEnd    return NilEndfunctionWriteFile (filename, data, RT)Localf =assert(Io.open(filename, RT)) F:write (data) f:close ()EndfunctionCheckdirok (path)LocalPrepath =Lfs.currentdir ()ifLfs.chdir (PATH) ThenLfs.chdir (Prepath)return true    End    ifLfs.mkdir (PATH) Then        return true    EndEnd

--Read

if  not  then        return    End    local cnt = makefileutils.readfile (FN)    ifniland"" then         return  json.decode (CNT)    End

--Write

1 makefileutils.writefile (FN, Json.encode (CNT), "w+")

Quick-cocos2d-x data storage Userdefault gamestate IO

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.