godot-third-person-basic-scene/scripts/user_settings.gd

117 lines
3.5 KiB
GDScript3
Raw Normal View History

extends Node
var cfg_path = "user://settings.cfg"
2022-05-24 21:32:25 +00:00
var updated = false
@onready var configuration = get_settings()
func save():
2022-05-24 21:32:25 +00:00
if not updated:
return
Common.msg_debug("Save to config file : %s/%s" % [OS.get_user_data_dir(), cfg_path.replace("user://", "")])
2022-05-24 21:32:25 +00:00
Common.msg_debug(str(configuration))
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)
2022-05-24 21:32:25 +00:00
Common.msg_debug("Saved to config file : %s/%s" % [OS.get_user_data_dir(), cfg_path.replace("user://", "")])
2022-05-24 21:32:25 +00:00
func get_settings() -> Dictionary:
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():
2022-05-24 21:32:25 +00:00
Common.msg_debug ("Config section : %s" % section)
for parameter in config.get_section_keys(section):
2022-05-24 21:32:25 +00:00
Common.msg_debug("Config parameter : %s" % parameter)
if not cfg.has(section):
cfg[section] = {}
cfg[section][parameter] = config.get_value(section, parameter)
Common.msg_debug("After load config: " + str(cfg))
return cfg
2022-05-24 21:32:25 +00:00
func set_float_group_key(group:String, key:String, value:float):
Common.msg_debug("Set config[" + group + "][" + key + "] = " + str(value))
if not configuration.has(group):
configuration[group] = {}
if configuration[group].has(key):
if configuration[group][key] == value:
return
updated = true
configuration[group][key] = value
func get_float_group_key(group:String, key:String, default:float) -> float:
if configuration.has(group):
if configuration[group].has(key):
if typeof(configuration[group][key]) == TYPE_FLOAT:
return configuration[group][key]
Common.msg_debug("type:" + str(typeof(configuration[group][key])))
set_float_group_key(group, key, default)
return default
func set_bool_group_key(group:String, key:String, value:bool):
Common.msg_debug("Set config[" + group + "][" + key + "] = " + str(value))
if not configuration.has(group):
configuration[group] = {}
if configuration[group].has(key):
if configuration[group][key] == value:
return
updated = true
configuration[group][key] = value
func get_bool_group_key(group:String, key:String, default:bool) -> bool:
if configuration.has(group):
if configuration[group].has(key):
if typeof(configuration[group][key]) == TYPE_BOOL:
return configuration[group][key]
Common.msg_debug("type:" + str(typeof(configuration[group][key])))
set_bool_group_key(group, key, default)
return default
func set_sound_music_volume(value: float):
return set_float_group_key("sound", "music_volume", value)
func get_sound_music_volume() -> float:
return get_float_group_key("sound", "music_volume", 100.0)
func set_sound_global_volume(value: float):
return set_float_group_key("sound", "global_volume", value)
func get_sound_global_volume() -> float:
return get_float_group_key("sound", "global_volume", 100.0)
func set_sound_mute(value: bool):
return set_bool_group_key("sound", "mute", value)
func get_sound_mute() -> bool:
return get_bool_group_key("sound", "mute", false)
func set_sound_music_play(value: bool):
return set_bool_group_key("sound", "music_play", value)
func get_sound_music_play() -> bool:
return get_bool_group_key("sound", "music_play", true)
func set_sound_music_random(value: bool):
return set_bool_group_key("sound", "music_random", value)
func get_sound_music_random() -> bool:
return get_bool_group_key("sound", "music_random", true)