Lua data is stored as table tables

Source: Internet
Author: User

Source: http://blog.sina.com.cn/s/blog_991afe570101rkfh.html

Thanks, the original author's selfless dedication!

Implementation ideas:

Table tables------> converted to strings (using the JSON library here)-------> Store---------> Read

To achieve the purpose:

    • Solve string stitching problems

    • The simplest way to save table tables to files, game archives, game levels, etc.

First step: Convert Table Table to JSON-formatted string

Import Header File

--2.2.5 's quick version has completed the initialization of the JSON module when the framework is loaded, so there is no need to do the import header file--local JSON = require ("Framework.shared.json")


Create a table

Local t = {} t.test = "Okok" t.net = "Okok" T.inner = {} t.inner.test = "OKOK8888"--mainly implemented using the following two methods for local Wirtjson =json.encode (t)--Convert to JSON format local Readjson =json.decode (Wirtjson)--Go to the original format before storage

Note: The following two methods can be obtained to the current computer on the root of the project

1, Device.writablepath

2 , Device.cachepath

The second step: write to the file, store and read

The first approach: the LUA IO library    

Save the file to the current project root directory:

Local test = assert (Io.open (Device.writablepath. "/formation.lua", "W")) Test:write (Wirtjson) test:close ()

To read the entire file:

Local test = Io.open (Device.writablepath. "/formation.lua", "R") Local readjson= test:read ("*a") local table =json.decode (Readjson) test:close ()

The second method: Ccuserdefault

Save the file to the current project root directory:

Ccuserdefault:shareduserdefault (): Setstringforkey ("SaveFightArray1", Wirtjson) Ccuserdefault:shareduserdefault () : Flush ()

To read the entire file:

Local ecord1 = Ccuserdefault:shareduserdefault (): Getstringforkey ("SaveFightArray1") local table =json.decode (record1 )

Lua data is stored as table tables

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.