Merge branch '25-add-a-configuration-file-to-save-user-preferences' into 'main'

Resolve "Add a configuration file to save user preferences"

Closes #25

See merge request yannk/godot-third-person-basic-scene!2
This commit is contained in:
aleajactaest 2022-05-24 22:56:24 +02:00
commit 5720ff1def
5 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,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()) * 100.0))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(int(db2linear($Music.get_volume_db()) * 100.0)))
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)
@ -221,7 +223,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))

View file

@ -6,6 +6,7 @@
script = ExtResource( "1_dltpv" )
[node name="Music" type="AudioStreamPlayer" parent="."]
volume_db = 23.803
[node name="EndMusic" type="Timer" parent="."]

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/`

25
scripts/user_settings.gd Normal file
View file

@ -0,0 +1,25 @@
extends Node
var cfg_path = "user://settings.cfg"
@onready var configuration = get_settings()
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():
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