-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydata.lua
36 lines (31 loc) · 921 Bytes
/
mydata.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- used for handling global data
local M = {score=0, bestScore=0, filename="scorefile.txt"}
function M.setBestScore( )
if M.score > M.getBestScore( ) then
local path = system.pathForFile( M.filename, system.DocumentsDirectory)
local file = io.open(path, "w")
if file then
local contents = tostring( M.score )
file:write( contents )
io.close( file )
return true
else
print("Error: could not read ", M.filename, ".")
return false
end
end
end
function M.getBestScore()
local path = system.pathForFile( M.filename, system.DocumentsDirectory)
local contents = ""
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
local score = tonumber(contents);
io.close( file )
return score
end
return 0
end
return M