-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame_highscore.gd
72 lines (56 loc) · 1.58 KB
/
game_highscore.gd
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
69
70
71
72
extends Node
# Variables
var db;
var highscore = 0;
var row_id = 0;
@onready
var open = false;
func _ready():
# Create SQLite instance
db = SQLite.new()
# Open the database
if not db.open("user://player_stats.sqlite"):
return
open = true
var query: SQLiteQuery = db.create_query("CREATE TABLE IF NOT EXISTS highscore (id INTEGER PRIMARY KEY, score INTEGER NOT NULL);")
# Create table
if not query.execute([]).is_empty():
return
query = db.create_query("SELECT id, score FROM highscore LIMIT 1;")
# Retrieve current highscore
var rows = query.execute()
if (rows and not rows.is_empty()):
row_id = rows[0][0];
highscore = rows[0][1];
# Test
set_highscore(1000)
set_highscore(2000)
set_highscore(10000)
set_highscore(50000)
print("High score: ", get_highscore())
func _exit_tree():
if db:
# Close database
db.close()
func set_highscore(score):
if not open:
return
# Update highscore
highscore = score
# Execute sql syntax
if row_id > 0:
db.create_query("UPDATE highscore SET score=? WHERE id=?;").execute([highscore, row_id])
else:
db.create_query("INSERT INTO highscore (score) VALUES (?);").execute([row_id])
var query = db.create_query("SELECT last_insert_rowid()")
row_id = query.execute([])[0][query.get_columns().find("last_insert_rowid()")]
func get_highscore():
if not open:
return
var query: SQLiteQuery = db.create_query("SELECT score FROM highscore WHERE id=? LIMIT 1;");
# Retrieve highscore from database
var rows = query.execute([row_id])
if (rows and not rows.is_empty()):
highscore = rows[0][0]
# Return the highscore
return highscore