Continuation of this: [Accepted Answer] How do I change Config Values from within the game?.
I just encountered a new error in the config.gd script.
The most peculiar part about this error is that the now-erroneous code worked just yesterday, with the error only appearing after I was working on an unrelated part of the game
(I was adding a missing Sound Effect to an item in the game by adding an AudioStreamPlayer2D to it and calling "./AudioStreamPlayer2D".playing = true
when appropriate).
I could understand if the error appeared after a Godot software update, but I downloaded Godot from the official webpage months ago, so the apt update
and apt upgrade
commands wouldn't update Godot since I manually dragged it into the /bin/ folder instead of installing it from the apt
repository.
This is the config.gd error:
E 0:00:00.394 call: Error parsing JSON at line 0: Expected '}' or ','
- <C++ source code>modules/gdscript/gdscript_functions.cpp:1268 @ call()
- <​Stacktrace> config.gd:67 @ file_load()
- config.gd:30 @ _ready()
It is not fatal, but it Does stop the game when it happens.
If I keep pressing the Pause button in the editor, or play the game on my Android Phone, I can still play it perfectly,
with the bug that the game would no longer properly load the config file.
This is the config.gd script:
extends Node
const USER_CONFIG_PATH = "user://config.cfg"
var _config_file = ConfigFile.new()
var audio_bus_names = ["music", "sfx"]
var _settings = {
"audio_volume": #in db
{
"music": AudioServer.get_bus_volume_db(
AudioServer.get_bus_index("music") ),
"sfx": AudioServer.get_bus_volume_db(
AudioServer.get_bus_index("sfx") )
} }
func _ready():
# warning-ignore:return_value_discarded
file_load(USER_CONFIG_PATH)
for i in audio_bus_names:
AudioServer.set_bus_volume_db (
AudioServer.get_bus_index(i),
config._settings["audio_volume"][i]
)
func _fixed_process():
var error_load = config.load(USER_CONFIG_PATH)
if error_load == OK:
pass
# warning-ignore:unused_variable
#var audio_volume_music = config.get_value("audio_volume", "music", 0)
# warning-ignore:unused_variable
#var audio_volume_sfx = config.get_value("audio_volume", "sfx", 0)
else:
config.set_value("audio_volume", "music", 0)
config.set_value("audio_volume", "sfx", 0)
config.save_file(USER_CONFIG_PATH)
func file_save(filepath):
var save_config = File.new()
save_config.open(filepath, File.WRITE)
save_config.store_line(to_json(_settings))
save_config.close()
func file_load(filepath):
var save_config = File.new()
if ( save_config.file_exists(filepath) ):
save_config.open(filepath, File.READ)
_settings = parse_json( save_config.get_line() ) #The now-erroneous line (Error Message above)
save_config.close()
I also got another error message in the Volume Slider script, which also worked just yesterday, with it somehow now giving out an error message it didn't yesterday, despite the code remaining unchanged compared to when it still worked:
Invalid get index 'audio_volume' (on base: 'Nil').
extends HSlider
export (String) var bus_name = "Master"
func _ready():
$".".value = db2linear(config._settings["audio_volume"][bus_name])
func _on_slider_value_changed(value):
AudioServer.set_bus_volume_db(
AudioServer.get_bus_index(bus_name), linear2db(value)
)
config._settings["audio_volume"][bus_name] = linear2db(value)
print( bus_name, ", ", config._settings["audio_volume"][bus_name] )
config.file_save(config.USER_CONFIG_PATH)