Hi, I’m trying to create a mute option so that when the user presses it and exits the game, the mute setting remains active when they log back in.
I’ve created a button that mutes the game and a resource, but I can’t get it to save the information. Any suggestions or alternative methods?
code of the mute button
extends CheckButton
func _on_toggled(toggled_on: bool) -> void:
var bus_master = AudioServer.get_bus_index("Master")
AudioServer.set_bus_mute(bus_master, not AudioServer.is_bus_mute(bus_master) )
ScoreManager._mute = bus_master
code of the scoremanager related to the resource
var mute
var _mute :
get:
return mute
set(value):
mute = value
save_option()
func load_option()->void:
if ResourceLoader.exists(options_path) == true:
var ops:optionsResource= load(options_path)
if ops:
pass
func save_option()-> void:
var ops: optionsResource = optionsResource.new()
ResourceSaver.save(ops,options_path)
resource code
class_name optionsResource
extends Resource
@export var mute := AudioServer.get_bus_index("Master")
I recommend using a ConfigFile instead to save settings, that’s what it’s for, and it’s human readable enough where your players can modify the file themselves if needed.
You could also use my Disk Plugin. (Installation instructions in the link.) Then just do this:
In your ScoreManager
var mute: bool = Disk.load_setting("Muted")
set(value):
mute = value
Disk.save_setting(mute, "Muted")
In your button:
extends CheckButton
func _on_toggled(toggled_on: bool) -> void:
var bus_master = AudioServer.get_bus_index("Master")
AudioServer.set_bus_mute(bus_master, not AudioServer.is_bus_mute(bus_master) )
ScoreManager.mute = bus_master
Note I removed all the _mute code because since you’re modifying it outside your function, you don’t need a private variable, and you shouldn’t modify a private variable from another function.
When you load the game, the ScoreManager will load your setting from disk, and if there is none, it’ll be false by default. As soon as you set it, it’ll be saved in a configuration settings file for you.
If you need something more complex, you could also check out my Sound Plugin.