Adding basic functions to handle music volume with a config file to save user preferences

This commit is contained in:
yannk 2022-04-21 23:51:45 +02:00
parent f451c4e3d4
commit 3ce18ecbcd
4 changed files with 38 additions and 3 deletions

View file

@ -17,6 +17,7 @@ config/features=PackedStringArray("4.0", "Vulkan Clustered")
[autoload]
user_settings="*res://scripts/user_settings.gd"
Themes="*res://scripts/themes.gd"
Common="*res://scripts/common.gd"
Screenshot="*res://scripts/screenshot.gd"

View file

@ -42,8 +42,8 @@ func _ready():
# var t = linear2db(0.0)
# var u = db2linear($Music.get_volume_db())
# Common.msg_debug("volume :" + str(u))
$Window/VBox/Tab/Mixer/MusicLevel/music.set_value( int(db2linear($Music.get_volume_db()) * 100.0))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(int(db2linear($Music.get_volume_db()) * 100.0)))
$Window/VBox/Tab/Mixer/MusicLevel/music.set_value( int(db2linear($Music.get_volume_db()) * float(user_settings.configuration["sound"]["music_volume"])))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(int(db2linear($Music.get_volume_db()) * float(user_settings.configuration["sound"]["music_volume"]))))
var bus_name = $Music.get_bus()
Common.msg_debug("bus_name: " + str(bus_name))
var bus_id = AudioServer.get_bus_index(bus_name)
@ -221,7 +221,8 @@ func _on_effect_value_changed(value):
func _on_music_value_changed(value):
$Music.set_volume_db(linear2db(value/100.0))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(value))
user_settings.configuration["sound"]["music_volume"] = value
user_settings.save()
func _on_global_value_changed(value):
AudioServer.set_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()), linear2db(value/100.0))

5
scripts/README.md Normal file
View file

@ -0,0 +1,5 @@
# Folder for configuration file
By default, Godot places the "user://" folder there :
- on Linux : `~\.local/share/godot/app_userdata/Third Person basic scene/`

28
scripts/user_settings.gd Normal file
View file

@ -0,0 +1,28 @@
extends Node
var cfg_path = "user://settings.cfg"
@onready var configuration = get_settings()
# Called when the node enters the scene tree for the first time.
func _ready():
pass
func save():
var file = ConfigFile.new()
for section in configuration.keys():
for key in configuration[section].keys():
file.set_value(section, key, configuration[section][key])
file.save(cfg_path)
func get_settings():
var cfg = {}
var config = ConfigFile.new()
var cfg_file_content = config.load(cfg_path)
if cfg_file_content != OK:
return cfg
for section in config.get_sections():
print ("Config section : %s" % section)
for parameter in config.get_section_keys(section):
print("Config parameter : %s" % parameter)
cfg[section] = {parameter:config.get_value(section, parameter)}
return cfg