Read/write variables to file (persisting file)

Hey there :slight_smile:

Can anyone show me an example of reading/writing variables and values to a persisting file? I’ve tried a couple of times but havent found a working solution!

Any guru out there willing to help? [import]uid: 71002 topic_id: 17824 reply_id: 317824[/import]

Hi there,

You can do it in multiple ways, but here is a method that will save a value to a text file (that persists) and then the function below will load the value from the same file.

--This is the variable we are saving and loading to.  
local totalThrows = 0  
  
--Function to save totalThrows to file..  
local function saveThrows()  
 local filePath = system.pathForFile( "throws.txt", system.DocumentsDirectory )  
 local file = io.open( filePath, "w" )  
  
 file:write( totalThrows )   
 io.close( file )  
end  
saveThrows() --Use this when you need to save the throws..  
  
--Function to load totalThrows from file..  
local function loadThrows()  
 local filePath = system.pathForFile( "throws.txt", system.DocumentsDirectory )  
 local file = io.open( filePath, "r" )  
  
 if file then  
 local contents = file:read( "\*a" )  
 totalThrows = contents  
 io.close( file )  
 else  
 file = io.open( filePath, "w" )  
 file:write( totalThrows )  
 io.close( file )  
 return totalThrows  
 end  
end   
loadThrows() --Then call this when you want to load throws..  

If you need me to go into more detail with the above, then just say :slight_smile: [import]uid: 69826 topic_id: 17824 reply_id: 68017[/import]

Heres another way using databases…

This is abit more complicated, but you can put alot of information into a database, therefore saving lots of different variables if you wanted. Below is a quick example of creating/updating/loading from a database.

--Need to require this somewhere to use Databases..  
local sqlite3 = require("sqlite3")  
local totalThrows = 0 --Variable that we are saving/loading to.  
--Create the database to be used...  
local function setupDatabase()  
 local dbPath = system.pathForFile("throwInfo.db3", system.DocumentsDirectory)  
 local db = sqlite3.open( dbPath )   
  
 local tablesetup = [[   
 CREATE TABLE throws (id INTEGER PRIMARY KEY, amount);  
 INSERT INTO throws VALUES (NULL, '0');  
 ]]  
 db:exec( tablesetup )  
 db:close()  
end  
setupDatabase()  
--Saves throws to a database that has already been created..  
local function saveThrows()  
 local dbPath = system.pathForFile("throwInfo.db3", system.DocumentsDirectory )  
 local db = sqlite3.open( dbPathA )   
  
 local update = "UPDATE throws SET amount = '"..totalThrows.."'"  
 db:exec(update)  
  
 db:close()  
end  
saveThrows()  
--Load throws from a database that has already been created..  
local function loadValue()  
 local dbPath = system.pathForFile("throwInfo.db3", system.DocumentsDirectory)  
 local db = sqlite3.open( dbPath )   
  
 for row in db:nrows("SELECT \* FROM throws'") do  
 totalThrows = row.amount  
 end  
 db:close()  
end  
loadValue()  

Also, bear in mind that i just typed this out in notepad and pasted it here, so it hasnt been tested, but i dont see why any of it wouldnt :smiley:

Hope some of this has helped. [import]uid: 69826 topic_id: 17824 reply_id: 68020[/import]