Problem with Volume Sliders
Old Title: Why do the Volume sliders reset after level transitions, but only visually?
When I change the Volume in One Level, the Sliders properly show the Volume of each bus (Left Image).
But after I enter the next level, the Volume remains the same but the Sliders show Full Volume (Right Image).
How could I fix this visual disconnect?
These are my Volume Changing Signals:
func _on_Sound_value_changed(value):
AudioServer.set_bus_volume_db(
AudioServer.get_bus_index("Sounds"),
linear2db(value)
)
func _on_Music_value_changed(value):
AudioServer.set_bus_volume_db(
AudioServer.get_bus_index("Music"),
linear2db(value)
)
Best Answer
-
TwistedTwigleg Posts: 3,856
You probably need to set the sliders when the scene is shown. If you have the sliders and you know what bus to use, then the code to retrieve the value should look something like this:
func _ready(): $SoundSlider.value = AudioServer.get_bus_volume_db( AudioServer.get_bus_index("Sounds") ) $MusicSlider.value = AudioServer.get_bus_volume_db( AudioServer.get_bus_index("Music") )
Edit: Fixed formatting.
Answers
And wheres the code handling the visible gui updating? Please show that too.
If you have none then you need to implement it. A function that is able set/update the sliders values.
Well, I've actually tried and failed that before. 🤷🏿♀️
You probably need to set the sliders when the scene is shown. If you have the sliders and you know what bus to use, then the code to retrieve the value should look something like this:
Edit: Fixed formatting.
Doing it in ready should do it yes.
@TwistedTwigleg Thanks very much! Now I've gotten slightly further to fixing my problem.
But it's not finished yet:
The Volume Sliders DO now show the accurate volume upon loading a new scene,
but it's only because the Audio gets muted and the Sliders get reset to Minimum after every time a level gets loaded.
I still need the Volume to remain between Scene Loadings. But for that, I'll make a new Discussion if you aren't too fast to answer.
Sounds like something is changing the volume to the minimum. What does the script for loading/changing scenes look like? Also, are you setting the audio level anywhere else? Once the AudioServer has an audio bus volume set, it should retain it unless it is overridden.
Also: while I understand what you are saying, please don't make the speed of my reply decide whether you make a new discussion or not. Having the discussion here or making a new one is fine, though given it is related to this issue I think this discussion may be better, but regardless please do not make my hastiness or lack thereof be the deciding factor. Thanks
As far as I'm aware, nothing is overriding the AudioBus Volumes.
Before I followed the answer:
Only after I applied the answer, the Audio gets silenced after each Loading.
The're's no One Script for Loading a level.
This is how the Player Script resets the level:
This is how the Exit Script loads the Next Level:
This sounds like really good forum advice.
I wanted to make it a new discussion if the reply didn't came already, because I was not sure if it was a good practice to ask related but different questions in the comments of the same question.
Especially since the forum has a linear comment flow as suppose to starting mini-threads by replying to individual comments.
Hmm, so it doesn't seem to be caused by the scene changing. In the
_ready
function, maybe try adding the following print statement and code:Then you can see if it is the slider value assignment causing the issue, or whether the value being retrieved by the audio server is causing the issue. That should, hopefully, help pin down the issue.
I basically already had this code: The only difference is, that the
_ready(): print()
was missing.After every Loading, the Console displays One more line of
-inf, -inf
,which corresponds to the Volumes muting to -Infinity after every Loading.
Maybe remove the use of
linear2db
when setting the volume in the AudioServer and see if that fixes it? From what I can tell,linear2db
is for converting a normalized float into a db range, as seen in this Godot engine blog post (which granted, is on the older side).@TwistedTwigleg If I remove
linear2db
, the Volume is permanantly set to Full VolumeNow I wanted to try saving and retrieving the Volume Slider Values to a ConfigFile,
but I failed to understand how it works.
Hmm, at least we know that is not the issue.
The ConfigFile is really interesting and powerful, but unfortunately there is really not a lot of material covering it. That said, something like this should work for saving and loading the audio:
Ideally you'd only save changes once you are about to change scenes, but the code above should at least give an idea about how to go about using a ConfigFile.
ConfigFile.get_value()
accepts an optional 3rddefault
parameter which will be used if the value can't be found in the file. You can use this to avoid littering your code withif ConfigFile.has_section_key(...)
calls.This is way too much at once! Could I please get the changes explained step-by-step?
Sure, though I'm just going to explain each chunk instead of step-by-step, as it will be faster and easier to write, and it shouldn't impact the explanation any:
AUDIO_FILEPATH
is the filepath we are going to save the config file at. Theuser://
directory is a special Godot directory that maps to a location on the file system that can be read and written to by Godot. There's a page on the documentation if you are curious about where this file goes, which I can link to later if desired.config_file
is just a class variable that we'll be using to hold the reference to the ConfigFile, nothing fancy.First, this function makes a new ConfigFile and assigns it to the
config_file
variable. Then, we try to load the file atAUDIO_FILEPATH
, which is a location where the file will be stored. We assign the returned value of theload
function to a variable callederror
. Then we check to see iferror
is NOT equal toOK
, and this will occur if the file is not found, meaning no configuration has been saved yet. Iferror
is equal toOK
, then that means we have successfully loaded the config file.Nothing fancy here until the bit after the comment. What we are doing here is storing the new sound volume in the config file, in the section
audio
with the keysound_volume
. This means we can later access this value using the same section and key combo. Finally, we tell the config file to save itself so when we need to load it later, we have up-to-date values.First, we check to see if there is a value stored in the section
audio
with the keysound_volume
. If there is, then we get that value fromconfig_file
using theget_value
function and assign it to a function variable calledvolume
. We then assign the value of the slider to this value.If there is NOT a value stored in the section
audio
with the keysound_volume
, then we simply get the volume from the AudioServer like normal.However, as @Calinou mentioned, we can simplify this code by taking advantage of the
get_value
function. The third, optional, argument in theget_value
function will be returned if the key does not exist in theconfig_file
. So, we can make this third argument the AudioServer's sound instead, allowing us to use just two lines of code. If the value exists in theconfig_file
, then the new version will return it, while if it does not, the new version will simply return the third argument instead.Nothing fancy here until the bit after the comment, and its the same as the
_on_Sound_value_changed
function, just for music.Same thing here: It's the same code as
_on_Sound_ready
, it's just for music instead of sound. As I mentioned in the note above, you can simplify the code.Hopefully that helps explain things a bit!
@TwistedTwigleg @Calinou In both instances of the line
if (config_file.has_section_key( [...] )):
,I get an error message upon running the game:
The error message is saying that the
config_file
variable has not been assigned to anything, that it'snill
. Did you include the code in the_ready
function?@TwistegTwigleg Yes, this is the
_ready()
code:Am I now allowed to advertise the Game Repository?
I've been trying to avoid advertisment because advertisment is annoying,
but now I feel like it might be needed.
Not sure what you mean? Sharing your games repo, if you don't mind people getting access to it, is perfectly fine.
edit: that is to say it is at your own discretion. We don't particularly have rules against advertising things so long as it isn't rather obvious spamming.
What if I decide that I want people to have access to my game repository right now, but then I didn't foresee that at least 1 month later, I no longer want to have other people accessing it?
Is your repo on github? I think there might be settings to set it to private later? If I'm reading this right, that should be possible:
https://docs.github.com/en/enterprise/2.13/user/articles/setting-repository-visibility
I know how to set repositories to private. My question was, if I have to delete all mentions of the Repo when setting the Repo to Private.
If the repo is private then the links to it shouldn't be a problem I'd imagine. This is a forum, over time links are bound to be broken. That's just natural, sad as it may sometimes be.
This is really helpful!
I don't plan to private the Game Repository anytime soon, but I still asked, just to be safe.
This is the Game Repository: https://www.github.com/sosasees/flinkimaus
This is the issue which is relevant to fixing the Volume Sliders: https://github.com/sosasees/flinkimaus/issues/28
Now I fixed the problem described in the Title and Root Comment.
I still need help keeping the Volumes between Scenes.
I have made variables for the Sliders (soundVol, musicVol),
but I need to keep them between scenes.
Mostly, anything that works will suffice. I already tried making a ConfigFile, but it doesn't work because of the error
Not to sound very harsh and angry, but: Now I hope that you know why I wanted to make this a separate discussion: Now it takes up the majority of a discussion with a completely unrelated Title and Root Comment.
Actually, I think i WILL continue it in a new discussion.
EDIT: This link leads to the new discussion
That’s fair, I have no issue with new discussions. The only thing I had an issue with previously (in this thread) about making new discussions in this thread was making the timeliness of my reply being the deciding factor, not necessarily making a new thread. If you think it warrant’s a new discussion, then that’s fine. As I mentioned in that reply:
So, it’s all good