Mr. E
Mr. E

Reputation: 75

How do I read/get values from a file in corona SDK?

I am currently making an app in corona SDK. My goal for now is to create something (like a string or Boolean) that could be stored in a .txt file. What I want to do is in one, let us say for example scores.lua file have all the values and then, when in need use them in the main.lua file. The problem is that the main.lua does not get the files that I saved in scores.lua.

I am using something called ego.lua

    function saveFile( fileName, fileData ) 
    local path = system.pathForFile( fileName, system.DocumentsDirectory )  
    local file = io.open( path, "w+" )


if file then
   file:write( fileData )
   io.close( file )
 end
end


function loadFile( fileName )    
local path = system.pathForFile( fileName, system.DocumentsDirectory )    
local file = io.open( path, "r" )


if file then
   local fileData = file:read( "*a" )
   io.close( file )
   return fileData  
else
   file = io.open( path, "w" )
   file:write( "empty" )
   io.close( file )
   return "empty"
  end
 end

and what I save in my main.lua file:

ego = require "ego"
saveFile = ego.saveFile
loadFile = ego.loadFile

valueName = loadFile( "gucci.txt" )
local money = display.newText(tostring(valueName), 200, 100, "Helvetica", 20)

and my score.lua file :

ego = require "ego"
saveFile = ego.saveFile
loadFile = ego.loadFile

saveFile( "gucci.txt", "This works")

Upvotes: 0

Views: 256

Answers (1)

ldurniat
ldurniat

Reputation: 1702

I recommended you Simple-Table-Load-Save-Functions-for-Corona-SDK - Two very simple load and save function to store a Lua Table and Read it back in. Requires the Corona SDK JSON Library.

Upvotes: 1

Related Questions