-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicSave.gd
61 lines (43 loc) · 1.08 KB
/
BasicSave.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
extends Node
var save_file = "user://save_game.save"
var saved_data = {}
const UUID = preload("res://analytics/uuid.gd")
func init():
load_game()
saved_data.session_num += 1
save_game()
print(saved_data)
return saved_data
func save_data():
var save_dict = {
"user_id": get_user_id(),
"game_version": "0.0.2",
"session_num": get_session_num()
}
return save_dict
func save_game():
var save_data = save_data()
var save_game = File.new()
save_game.open(save_file, File.WRITE)
save_game.store_line(to_json(save_data))
save_game.close()
func load_game():
var save_game = File.new()
if not save_game.file_exists(save_file):
saved_data = save_data()
return
save_game.open(save_file, File.READ)
saved_data = parse_json(save_game.get_as_text())
save_game.close()
var new_info = save_data()
for d in new_info:
if not saved_data.has(d):
saved_data[d] = new_info[d]
func get_session_num():
if saved_data.has('session_num'):
return saved_data.session_num
return 0
func get_user_id():
if saved_data.has('user_id'):
return saved_data.user_id
return UUID.v4()