From 3ce18ecbcd95e5e1061c9e48738424758142f7ed Mon Sep 17 00:00:00 2001 From: yannk Date: Thu, 21 Apr 2022 23:51:45 +0200 Subject: [PATCH 1/2] Adding basic functions to handle music volume with a config file to save user preferences --- project.godot | 1 + scenes/music/music_manager.gd | 7 ++++--- scripts/README.md | 5 +++++ scripts/user_settings.gd | 28 ++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 scripts/README.md create mode 100644 scripts/user_settings.gd diff --git a/project.godot b/project.godot index bff5a98..c8e5d8b 100644 --- a/project.godot +++ b/project.godot @@ -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" diff --git a/scenes/music/music_manager.gd b/scenes/music/music_manager.gd index c28f698..a519ac0 100644 --- a/scenes/music/music_manager.gd +++ b/scenes/music/music_manager.gd @@ -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)) diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 0000000..e24dba2 --- /dev/null +++ b/scripts/README.md @@ -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/` \ No newline at end of file diff --git a/scripts/user_settings.gd b/scripts/user_settings.gd new file mode 100644 index 0000000..267c5be --- /dev/null +++ b/scripts/user_settings.gd @@ -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 From 52d8bdf306201f911b4c4665c9f1b2fa4ab4c7a2 Mon Sep 17 00:00:00 2001 From: yannk Date: Fri, 22 Apr 2022 09:58:30 +0200 Subject: [PATCH 2/2] Implementing first use of configuration file with music volume --- scenes/music/music_manager.gd | 6 ++++-- scenes/music/music_manager.tscn | 1 + scripts/user_settings.gd | 5 +---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scenes/music/music_manager.gd b/scenes/music/music_manager.gd index a519ac0..e7b3f43 100644 --- a/scenes/music/music_manager.gd +++ b/scenes/music/music_manager.gd @@ -42,8 +42,10 @@ 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()) * 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 music_volume = float(user_settings.configuration["sound"]["music_volume"]) + $Music.set_volume_db(linear2db(music_volume/100.0)) + $Window/VBox/Tab/Mixer/MusicLevel/music.set_value( int(db2linear($Music.get_volume_db()) * 100)) + $Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(int(db2linear($Music.get_volume_db()) * 100))) var bus_name = $Music.get_bus() Common.msg_debug("bus_name: " + str(bus_name)) var bus_id = AudioServer.get_bus_index(bus_name) diff --git a/scenes/music/music_manager.tscn b/scenes/music/music_manager.tscn index eeadc05..4c75b49 100644 --- a/scenes/music/music_manager.tscn +++ b/scenes/music/music_manager.tscn @@ -6,6 +6,7 @@ script = ExtResource( "1_dltpv" ) [node name="Music" type="AudioStreamPlayer" parent="."] +volume_db = 23.803 [node name="EndMusic" type="Timer" parent="."] diff --git a/scripts/user_settings.gd b/scripts/user_settings.gd index 267c5be..e40bd3f 100644 --- a/scripts/user_settings.gd +++ b/scripts/user_settings.gd @@ -3,11 +3,8 @@ 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(): + Common.msg_debug("Save to config file : %s/%s" % [OS.get_user_data_dir(), cfg_path.replace("user://", "")]) var file = ConfigFile.new() for section in configuration.keys(): for key in configuration[section].keys():