当前位置:网站首页>Data is stored in the form of table

Data is stored in the form of table

2022-07-05 04:57:00 Meteor spot

LUA Learning notes ----- The data to table Table storage

(2013-08-23 16:39:10)
  classification :LUA Script development
Realize the idea :
table surface ===》》》 To string ( Here use json library )===》》》 Storage ===》》》 Read
 
To achieve an end
  • Solve the string splicing problem
  • In the simplest way table Table stored in file , Convenient for game archiving 、 Game level 、 wait
First step : take table Table to json Format string
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"
 
local wirtjson =json.encode(t)-- convert to json Format
local readjson =json.decode(wirtjson)-- Convert to the original format before storage
 
Be careful : The following two methods can get the root directory of the project on the current computer LUA Learning notes ----- The data to table Table storage
  1.  device.writablePath          
  2.  device.cachePath  
The second step : write file , Store and read
  The first method :lua io library       
Save the file to the root directory of the current project :
local test = assert(io.open(device.writablePath .. "/formation.lua", "w"))
test:write(wirtjson)
test:close()
 
Read 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 root directory of the current project :
CCUserDefault:sharedUserDefault():setStringForKey("SaveFightArray1", wirtjson)
CCUserDefault:sharedUserDefault():flush()
Read entire file :
record1 = CCUserDefault:sharedUserDefault():getStringForKey("SaveFightArray1")
local  table =json.decode(record1)
原网站

版权声明
本文为[Meteor spot]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140625237836.html