android - How to create and save settings in corona sdk? -
i using method save settings game
when should use savetabel , loadtable
if use savetable when app starts saves default valeus of table, how can load last saved valeus when app starts again.
can use( if )to check whether file exist or not ?
some please
thanks in advance!
you should load file default values , if file doesn't exist should create it. , every time change value save value in file.
following code may you:
function load_settings() local path = system.pathforfile( "savesettings.json", system.documentsdirectory ) local file = io.open( path, "r" ) if file local savedata = file:read( "*a" ) io.close( file ) local jsonread = json.decode(savedata) value = jsonread.value else value = 1 end end function save_settings() local savegame = {} if value savegame["value"] = value end local jsonsavegame = json.encode(savegame) local path = system.pathforfile( "savesettings.json", system.documentsdirectory ) local file = io.open( path, "w" ) file:write( jsonsavegame ) io.close( file ) file = nil end
just call these functions loading , saving data. , easier if code these functions in different file , every time of loading , saving require file , use these functions.
Comments
Post a Comment