-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighscore.lua
68 lines (47 loc) · 1.28 KB
/
highscore.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local highScore = {}
function highScore:new( o )
o = o or {}
setmetatable( o, self )
self.__index = self
return o
end
function highScore:getHigScore()
local currentHighScore = 0
-- Path for the file to read
local path = system.pathForFile( "highScore.txt", system.DocumentsDirectory )
-- Open the file handle
local file, errorString = io.open( path, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Read data from file
local contents = file:read( "*a" )
-- Output the file contents
print( "Contents of " .. path .. "\n" .. contents )
currentHighScore = contents
-- Close the file handle
io.close( file )
end
file = nil
return currentHighScore
end
function highScore:setHigScore(currentHighScore)
-- Data (string) to write
local saveData = currentHighScore
-- Path for the file to write
local path = system.pathForFile( "highScore.txt", system.DocumentsDirectory )
-- Open the file handle
local file, errorString = io.open( path, "w" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Write data to file
file:write( saveData )
-- Close the file handle
io.close( file )
end
file = nil
end
return highScore