fuzzyworld

godot-shooter / asteroids / files / utils / Settings.gd

Branch asteroids · default branch · all branches

Settings.gd

891 B · history · blame

1class_name Settings
2
3var show_intro = true
4var show_items = false
5var high_scores: Array = []
6
7class HighScore:
8 var score: int
9 var date: Dictionary
10 func _init(s: int, d: Dictionary):
11 score = s
12 date = d
13 func to_json() -> Dictionary:
14 return {
15 "score": score,
16 "date": date
17 }
18
19const settings_path = "user://settings.json"
20
21func to_json() -> Dictionary:
22 var scores = []
23 for hs in high_scores:
24 var high_score: HighScore = hs
25 var json = high_score.to_json()
26 scores.push_back(json)
27 var final_obj = {
28 "show_intro": show_intro,
29 "show_items": show_items,
30 "high_scores": scores
31 }
32 return final_obj
33
34func save_user_settings(path = settings_path) -> void:
35 var save_game: File = File.new()
36 var _err = save_game.open(path, File.WRITE)
37 var json_data = JSON.print(self.to_json())
38 print("saving settings:", json_data)
39 save_game.store_string(json_data)
40 save_game.close()