Functions to read and write a complete table to disk.

I would like to see a simple set of functions to read a table from disk, and write it back to disk.

The files could be written in XML or even element format where the field name “=” field value with each field stored on a separate line would be good. Anything that could be stored in a table field should be able to be written and read back with these functions. I have done a little research and can’t find how to enumerate the table field names as text. this prevents is from writing our own functions to do the job. I know I could simply write them out, one field at a time but if something changes the field order, it would corrupt the function reading them back in.

the user should initialize the table in their preferred way in the event that the file does not exist when a read occurs. This would be the pre-initialization mode and would leave it the same way as it started if no file exists. The following code example could be a guide:

local xtable = {  
 element1 = 123,  
 userName= "",  
 highScore = 0  
}  
file = io.open ( path, "r" )  
result = file:readTable(xTable)  
io.close(file)  
file = io.open(path, "a")  
result = file:writeTable(xTable)  
io.close(file)  
  

Thanks!
stu [import]uid: 99429 topic_id: 37096 reply_id: 67096[/import]

Hi there,

Corona has built-in support for JSON, which is a standard format for storing information like this. Check out this tutorial from Rob about how to save and load data from Lua tables into JSON files: http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 37096 reply_id: 145343[/import]

I used to use the ICE library to save/load data, then moved onto GGData which is by the same guys.

However I’ll probably move onto this one for my next project, as GGData struggles with tables that aren’t a straight-forward structure:

http://developer.coronalabs.com/code/dmc-lib-auto-store [import]uid: 93133 topic_id: 37096 reply_id: 145390[/import]

I am looking at Rob’s solution to backing up a table using JSON.

Having this be part of the SDK makes sense, since 95% (my guess) of all apps have some type of saved data. The SDK has access to

I think the SDK could even have a data type called “persistent” that is saved and restored by the SDK would be a good idea.

Stu

[import]uid: 99429 topic_id: 37096 reply_id: 145406[/import]

Hi there,

Corona has built-in support for JSON, which is a standard format for storing information like this. Check out this tutorial from Rob about how to save and load data from Lua tables into JSON files: http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 37096 reply_id: 145343[/import]

I used to use the ICE library to save/load data, then moved onto GGData which is by the same guys.

However I’ll probably move onto this one for my next project, as GGData struggles with tables that aren’t a straight-forward structure:

http://developer.coronalabs.com/code/dmc-lib-auto-store [import]uid: 93133 topic_id: 37096 reply_id: 145390[/import]

I am looking at Rob’s solution to backing up a table using JSON.

Having this be part of the SDK makes sense, since 95% (my guess) of all apps have some type of saved data. The SDK has access to

I think the SDK could even have a data type called “persistent” that is saved and restored by the SDK would be a good idea.

Stu

[import]uid: 99429 topic_id: 37096 reply_id: 145406[/import]