corona - I have a loadFile error. Lua -


i trying implement highscore system game when try declare highscore loadfile error.

attempt call global 'loadfile' (a nil value) 

here code.

highscore = loadfile ("highscore.txt")  local function checkforfile ()     if highscore == "empty"         highscore = 0         savefile("highscore.txt", highscore)     end end checkforfile()  print ("highscore is", highscore)  local function onsystemevent ()     if playerscore > tonumber(highscore)         --we use tonumber highscore string when loaded         savefile("highscore.txt", score)     end end runtime:addeventlistener( "system", onsystemevent ) 

i using corona sdk.

the developers of corona ask published nice guide saving , writing files, should fulfill needs.

basically path via system.pathforfile , open using io.open.

you this:

local path = system.pathforfile( "highscore.txt", system.documentsdirectory ) local file = io.open(path, 'w+') 

and then, content of file:

local content = file:read('*a') local highscore  if (content ~= null)     highscore = tonumber(content)     // stuff loaded highscore end 

and write file:

file:write(highscore) 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -