Reading and Writing local files in Corona

hi.

I have read some of the tutorial and from the documentation, that it is possible to save game progress

using text files, using the json library from corona, encode it into a table, and then decode it again to 

be readable into lua language. 

I have also follow some of the tutorial to make the right write and save function, particularly what

Rob Miracle has posted in his blog:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

the question that I can’t get from googling is that, is it possible to save multiple files on one app? i’ve tried that, and it seems it can only write one file, and will overwrite the old one if i try to make a second local file.

I would assume that if your state save function is over-writing the same file all the time, that you are saving as the same filename.

There is no difficulty in saving multiple files with different names with Corona/Lua - take a look at my IO library to see what I mean:

http://code.coronalabs.com/code/io-lib

my code looks alike from what you have given me

local function loadStatus(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end M.loadStatus=loadStatus local function saveStatus(filename, save) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(save) file:write( contents ) io.close( file ) return true else return false end end M.saveStatus=saveStatus

what driving me nuts is that I can’t event know if it is actually saving, as i can’t open the file in the temp section 

of the project sandbox. at the few first on simulator it will work, but a few mins later it will not recognize the filename,

and when i print, it throw me the filename instead of a table reference

//main.lua \_played = lib.loadStatus('level\_status.json') print(\_played, 'played') //this one will print table reference number, //but later will just print level\_status.json if \_played == nil then print('check') \_played = {} for i = 1, 5 do \_played[i] = {} for j = 1, 21 do if j == 21 then \_played[i][j] = 0 end \_played[i][j] = 1 print('enter') end end lib.saveStatus('level\_status.json', \_played) end

giving some updates: it seems that i have referred to the wrong file library, which is the cause of unable to call the write/read function that i created properly. My bad :slight_smile:

I would assume that if your state save function is over-writing the same file all the time, that you are saving as the same filename.

There is no difficulty in saving multiple files with different names with Corona/Lua - take a look at my IO library to see what I mean:

http://code.coronalabs.com/code/io-lib

my code looks alike from what you have given me

local function loadStatus(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end M.loadStatus=loadStatus local function saveStatus(filename, save) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(save) file:write( contents ) io.close( file ) return true else return false end end M.saveStatus=saveStatus

what driving me nuts is that I can’t event know if it is actually saving, as i can’t open the file in the temp section 

of the project sandbox. at the few first on simulator it will work, but a few mins later it will not recognize the filename,

and when i print, it throw me the filename instead of a table reference

//main.lua \_played = lib.loadStatus('level\_status.json') print(\_played, 'played') //this one will print table reference number, //but later will just print level\_status.json if \_played == nil then print('check') \_played = {} for i = 1, 5 do \_played[i] = {} for j = 1, 21 do if j == 21 then \_played[i][j] = 0 end \_played[i][j] = 1 print('enter') end end lib.saveStatus('level\_status.json', \_played) end

giving some updates: it seems that i have referred to the wrong file library, which is the cause of unable to call the write/read function that i created properly. My bad :slight_smile: