mobile - Corona SDK data storage on simulator -


new corona sdk, , i'm trying figure out way load , save file (which stores game data) on simulator. (i dont want have debug on real device , take 15 seconds see variable change each time).

i followed tutorial on here: http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/ , couldn't find on stackoverflow addresses issue.

right have following code reading , storing files:

local readjsonfile = function( filename, base )      -- set default base dir if none specified     if not base base = system.resourcedirectory; end      -- create file path corona i/o     local path = system.pathforfile( filename, base )      -- hold contents of file     local contents      -- io.open opens file @ path. returns nil if no file found     local file = io.open( path, "r" )     if file        -- read contents of file string        contents = file:read( "*a" )        io.close( file ) -- close file after using     end      return contents end  local writetofile = function( filename, content )     -- set default base dir if none specified     if not base base = system.resourcedirectory; end      -- create file path corona i/o     local path = system.pathforfile( filename, base )      -- io.open opens file @ path. returns nil if no file found     local file = io.open( path, "w" )     if file        -- write contents of file string        file:write( content )        io.close( file ) -- close file after using     end end 

it seems work because i'll read json file, save different data, load it, , seems persist. however, close ide, changes gone. furthermore, actual file on system (mac book pro) not changing.

if do:

local json = require "json" local wordsdata = json.decode( readjsonfile( "trivia.txt" ) ) wordsdata.somekey = "something different" writetofile("trivia.txt", json.encode( wordsdata ) )  -- works temporarily 

i'm reading trivia.txt file in same directory main.lua , attempt change , load something. however, above code not make actual change trivia.txt on mac book pro.

what's proper way this?? need store game settings , game data (this trivia app, need store 50 words , answer user picked). need store data in such way when close ide, it'll remember wrote file.

my guess when load trivia.txt, it's looking @ mac book pro file, every time load ide. when run on simulator first time, creates new trivia.txt in temporary folder (which have no idea is). , start reading there if re-run same code. right?

any appreciated!!! upvotes more detailed answers, since i'm new corona sdk

i suggest use system.documentsdirectory path. first can read resource directory , can store in documentsdirectory. after can documentsdirectory. solve problem. here functions able check if file exist or not. can modify paths of course

function savetable(t, filename)     local path = system.pathforfile( filename, system.documentsdirectory)     local file = io.open(path, "w")     if file         local contents = json.encode(t)         file:write( contents )         io.close( file )         return true     else         return false     end end  function loadtable(filename)     local path = system.pathforfile( filename, system.documentsdirectory)     local mytable = {}     local file = io.open( path, "r" )     local contents = ""     if file         -- read contents of file string         local contents = file:read( "*a" )         mytable = json.decode(contents);         io.close( file )         return mytable     end     return nil end 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -