-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.gd
91 lines (61 loc) · 1.74 KB
/
options.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
extends Node
var keybinds = {} setget update_keybinds
var config = {} setget update_config
var savepath = "user://settings.save"
func update_keybinds(new):
keybinds = new
save_options()
func update_config(new):
config = new
update_audio_volume()
save_options()
func update_audio_volume():
for bus in ["master", "sfx", "music"]:
if !(bus in config):
config[bus] = 5
var curbus:int
match bus:
"master":
curbus = 0
"sfx":
curbus = 2
"music":
curbus = 1
AudioServer.set_bus_volume_db(curbus, linear2db(config[bus] / 10))
# AudioServer.set_bus_volume_db(_bus, linear2db(value))
func save_options():
#prepares the file
var saves = File.new()
saves.open(savepath, File.WRITE)
#vars to save
var vals = {
"keybinds": keybinds,
"config": config
}
saves.store_line(to_json(vals))
print("settings saved!")
saves.close()
#emit_signal("finish_save")
func update_controls_keybinds():
for keybind in keybinds:
InputMap.action_erase_event(keybind, InputMap.get_action_list(keybind)[0])
var event = InputEventKey.new()
event.scancode = keybinds[keybind]
InputMap.action_add_event(keybind, event)
func load_options():
var save_game = File.new()
if not save_game.file_exists(savepath):
yield(get_tree(), "idle_frame")
return # Error! We don't have a save to load.
# Load the file line by line and process that dictionary to restore
# the object it represents.
save_game.open(savepath, File.READ)
# Get the saved dictionary from the next line in the save file
var vals = parse_json(save_game.get_line())
for i in vals.keys():
set(i, vals[i])
save_game.close()
update_audio_volume()
update_controls_keybinds()
print("settings loaded!")
yield(get_tree(), "idle_frame")