-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveData.gd
More file actions
51 lines (39 loc) · 1.36 KB
/
SaveData.gd
File metadata and controls
51 lines (39 loc) · 1.36 KB
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
extends Node
const SAVE_PATH = "user://savegame.save"
var game_data := {}
func reset_data():
var save_game_file = File.new()
save_game_file.open(SAVE_PATH, File.WRITE)
print("Save File Path: %s" % save_game_file.get_path_absolute())
var level_count := 1
game_data = {}
for packedLevelScene in SceneManager.levels:
game_data[level_count as String] = { "is_locked": true, "is_difficult": false, "is_fully_cleared": false }
if level_count == 1:
game_data[level_count as String].is_locked = false
level_count += 1
save_game_file.store_line(to_json(game_data))
save_game_file.close()
func init_save_data():
var save_game_file = File.new()
if not save_game_file.file_exists(SAVE_PATH):
save_game_file.close()
reset_data()
func load_levels_data():
var save_file := File.new()
save_file.open(SAVE_PATH, File.READ)
game_data = parse_json(save_file.get_line())
save_file.close()
return game_data
func save():
var save_game_file = File.new()
save_game_file.open(SAVE_PATH, File.WRITE)
print("Saving Game Data: %s" % game_data)
save_game_file.store_line(to_json(game_data))
save_game_file.close()
func complete_current_level() -> void:
var next_level_s = (SceneManager.current_level + 1) as String
if game_data.has(next_level_s):
print("Updating %s (%s)" % [next_level_s, game_data[next_level_s]])
game_data[next_level_s].is_locked = false
save()