-
Notifications
You must be signed in to change notification settings - Fork 19
/
Quitting.bit
58 lines (48 loc) · 1.17 KB
/
Quitting.bit
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
package:application/vnd.bitty-archive;
data:text/json;count=144;path=info.json;
{
"id": 0,
"title": "Basics/03. Quitting",
"description": "",
"author": "Tony",
"version": "1.0",
"genre": "TUTORIAL",
"url": ""
}
data:text/lua;count=938;path=main.lua;
--[[
Example for the Bitty Engine
Copyright (C) 2020 - 2024 Tony Wang, all rights reserved
Homepage: https://paladin-t.github.io/bitty/
]]
-- Get a persistence file path under the writable directory.
local tmpPath = Path.combine(Path.writableDirectory, 'basics_quitting.txt')
print('Using: ', tmpPath)
-- Reads and returns persisted data from file.
local function read()
local result = 0
local file = File.new()
if file:open(tmpPath, Stream.Read) then
result = file:readLine()
result = tonumber(result)
file:close()
end
return result
end
-- Writes data to file.
local function write(data)
local file = File.new()
if file:open(tmpPath, Stream.Write) then
file:writeLine(tostring(data))
file:close()
end
end
local counter = 0
function setup()
counter = read() -- Read on setup.
print('Counter: ' .. counter)
end
function quit()
counter = counter + 1
write(counter) -- Write the new value to file on quit.
end