The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

Hey there!
I want to make a volume slider and it works quite well, except for one problem: When I return to the main menu, the volume control is set to the minimum (0) and I don't know what the problem is...
Here is the code I use to adjust the volume control (only this line causes the problem):

$Control/Options/Buttons/MusicVolume.value = AudioServer.get_bus_volume_db(AudioServer.get_bus_index("Music"));

I'm a noob btw

Godot version 3.2.3
in Engine by (12 points)

When are you setting the value? Most likely not in a place that it stays updated. Use print("updated") just before it and switch between scenes to see if it outputs

can you show us the _ready()-method of your main-menu-scene?

In the ready function in my mainmenu script

func _ready():
    AudioServer.set_bus_volume_db(AudioServer.get_bus_index("Music"), Global.music_volume);
    AudioServer.set_bus_volume_db(AudioServer.get_bus_index("SFX"), Global.sfx_volume);
    $Control/Options/Buttons/MusicVolume.value = AudioServer.get_bus_volume_db(AudioServer.get_bus_index("Music"));

don't forget that 0db == 100% volume

Do you mean in the AudioStreamPlayer? There I have Volume Db at 0

2 Answers

+1 vote

As whiteshampoo noted, the volume returned by get_bus_volume_db() is in decibels, which is not linear. If you want to control the volume with a slider, you should convert the decibels to linear values, using db2linear and linear2db. db2linear will return a value from 0 to 1, so you can multiply by 100 to get a range from 0 to 100 for you slider.

See[email protected]#class-gdscript-method-db2linear

by (643 points)

Oh wow, i didn't know this functions exist! THANK YOU"!
I always calulcated this myself xD

i already using db2linear(). The volume slider is not the problem, it works fine. The problem is that I want to keep the slider at the volume level after restarting the scene. Because otherwise it resets itself to the normal value from the slider.

+1 vote

I made you an example of how it should work.
There is btw no need to set the values in global-variables

extends Control

onready var VolumeSound : HSlider = $VolumeSound
onready var VolumeMusic : HSlider = $VolumeMusic

# Set sliders when scene is loading
func _ready() -> void:
    VolumeSound.value = get_linear_db("Sound") * VolumeSound.max_value
    VolumeMusic.value = get_linear_db("Music") * VolumeMusic.max_value

# Some methods to make everything a bit nicer to read
func get_linear_db(bus_name : String) -> float:
    assert(AudioServer.get_bus_index(bus_name) != -1, "Audiobus with the name " + bus_name + " does not exist.")
    return db2linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index(bus_name)))

func set_linear_db(bus_name : String, linear_db : float) -> void:
    assert(AudioServer.get_bus_index(bus_name) != -1, "Audiobus with the name " + bus_name + " does not exist.")
    linear_db = clamp(linear_db, 0.0, 1.0)
    AudioServer.set_bus_volume_db(AudioServer.get_bus_index(bus_name), linear2db(linear_db))

# Signals from sliders:
func _on_VolumeSound_value_changed(value: float) -> void:
    set_linear_db("Sound", value / VolumeSound.max_value)

func _on_VolumeMusic_value_changed(value: float) -> void:
    set_linear_db("Music", value / VolumeMusic.max_value)

Of course you have to adjust it to your scene.

by (1,536 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.