fuzzyworld

godot-shooter / files / utils / Settings.gd

Settings.gd

Blame: the commit that last changed each line · file · history

2464bc92 2023-11-14 Pavel Safronov1class_name Settings
2
3var show_intro = true
e96078c1 2023-11-17 Pavel Safronov4var show_items = false
5var high_scores: Array = []
f3c75ae9 2023-11-15 Pavel Safronov6
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 }
2464bc92 2023-11-14 Pavel Safronov18
19const settings_path = "user://settings.json"
20
21func to_json() -> Dictionary:
f3c75ae9 2023-11-15 Pavel Safronov22 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,
e96078c1 2023-11-17 Pavel Safronov29 "show_items": show_items,
f3c75ae9 2023-11-15 Pavel Safronov30 "high_scores": scores
2464bc92 2023-11-14 Pavel Safronov31 }
f3c75ae9 2023-11-15 Pavel Safronov32 return final_obj
2464bc92 2023-11-14 Pavel Safronov33
34func save_user_settings(path = settings_path) -> void:
35 var save_game: File = File.new()
f3c75ae9 2023-11-15 Pavel Safronov36 var _err = save_game.open(path, File.WRITE)
2464bc92 2023-11-14 Pavel Safronov37 var json_data = JSON.print(self.to_json())
f3c75ae9 2023-11-15 Pavel Safronov38 print("saving settings:", json_data)
39 save_game.store_string(json_data)
2464bc92 2023-11-14 Pavel Safronov40 save_game.close()