What went wrong on my volume slider

so i want to make 3 volume sliders i have the audio tab set up
image
i have made a scene called volume slider i have a hsilder and this code but for some reason only the master volume is working and the others are spaming the debug tab with line 17 and 12


here is the debugger

if anyone knows what i messed up and could help then thanks so much i’m just lost at what i did wrong

Although I haven’t done anything with audio buses, I played around with everything you showed here and discovered that AudioServer.get_bus_index() returns -1 if it can’t find a bus with the name represented by bus_name. Since -1 is outside the bounds of the internal array it’s looking through, you end up with an array index out of bounds error - could certainly be more descriptive.

Based on that, it looks like something about the way you’re acquiring/using each bus_name is broken, but that’s about all I can be sure about from here. As a test, maybe try hardcoding the value of bus_name in the function call and work backwards from there.

maybe i am tryna learn to code and this is what i picked up on from watching a few videos on audio buses and volume sliders but i dont know much else about this ill keep looking thru videos and see what i can find out about thanks so much BH67

1 Like

There’s definitely something missing in the configuration of the whole thing, beyond just the name string, but I don’t know what it is. AudioServer.get_bus_index(name) has a complement function, AudioServer.get_bus_name(index); when I gave that an argument of 0 and printed the result, I got Master as expected, and with an argument of anything else, the result was always -1 (I incremented a counter every frame and printed that result. Nothing).

Sorry about that; looks like you’ll have to do some more hunting, but at least you have a better idea of what’s going on now.

Edit: Ah, this might be something. I’m calling it quits for today, but I think it’s just that creating the bus does not automatically add it to AudioServer. Click the help file and the first member function in there is add_bus. Dollars to dimes says that’s your answer, or at least part of it =)

yeah thats true i understand it a bit more and hopefuly i can find a fix or a better way for the code to find my buses and make the slider work thanks alot you really helped me Bh67

i think this could be the fix i just added “” to line 10 witch is this value_changed.connect(_on_value_changed) but i added “” to it like this value_changed.connect("_on_value_changed") could that be the fix?

That should be fine without the quotes actually; I checked another project of mine. I think I have something that will work (couldn’t help but keep reading through the AudioServer class help file. I’m stubborn).

Instead of using the Audio tab at the bottom of the editor to add buses, try doing it in code. This produced an expected result of 1:

	AudioServer.add_bus(1)
	AudioServer.set_bus_name(1, "test")
	print(AudioServer.get_bus_index("test"))

Interestingly enough, this doesn’t seem to conflict with buses that you set up manually through the Audio tab; I have an AudioStreamPlayer set to play a sound from such a bus and it works fine. You might need to do that part in code now as well, but this should work.

Just make sure you don’t skip any index values along the way when you use add_bus(). 0 is already occupied by Master, and any further buses you add need to be incremented directly from there, so you might want to build some kind of setter function to help you with that. This should be enough to get you back on track =)

Ok, now I’m done, lol. Good luck!

thanks alot ill try and see what happends

few hours later i still dont know what is missing and cant find much on youtube

Did you check AudioServer’s help file yet? As in Ctrl + click on AudioServer in script? Youtube has its place, but I found everything right there actually.

Remembering this morning what your original objective was, I expanded on my tinkering from last night and in a few minutes I had a working volume slider. That’s not me trying to show off or be obnoxious mind you, but rather, I was surprised to find that it’s actually pretty straightforward and the docs show everything you need. Wherever you found that youtube video, they left half the code out.

You’ve done some course content, so play along now: here’s your challenge! ;D

  • If you haven’t already, reset your audio tab so that you ONLY have the Master bus.
  • Add an AudioStreamPlayer and give it a stream. Make the sound loop (or use music of a decent length) because this will be used for testing purposes. The loop setting is found on the sound resource itself in the FileSystem at the bottom left, not the AudioStreamPlayer.
  • In _ready(), add a new bus. Remember that the index of this new bus should be 1; see post 7 in this thread.
  • In _ready(), change your AudioStreamPlayer’s bus to the bus you added. You can do that with AudioStreamPlayer.bus and AudioServer.get_bus_name(). You may have to give your dynamically-created bus a name (again, see post 7) - I didn’t check what would happen if you don’t because it’s a good practice to name things anyway.
  • Connect your slider’s value_changed signal to a callback function. In that callback function, get the slider’s 1-100 value, convert that to a db value with a little range math, then use that to set the bus volume. You will need AudioServer.set_bus_volume_db(). You may also want to print what you set the volume to so that you can tweak your range math if needed (and don’t worry about blowing your speakers out. I accidentally set the volume to 8000 db and I was fine - Godot thankfully has a hard cap on this).

You should now have a working volume slider. You’re capable enough; I’m certain you can adapt this challenge to make it work with both your Music and SFX buses, as well as the Master bus.

Note that the Audio Bus documentation explains that as visualized in the Audio tab, buses connect from right to left. Because of this, the SFX bus in your original screenshot might incorrectly be set up to output into the Music bus; you would want both the Music and SFX buses to lead to the Master bus, since they are independent. AudioServer.set_bus_send() can make that change if necessary. Give this a quick read-through if that doesn’t make sense:

Thanks for making me learn how to use Audio Buses, lol, I’ll definitely be using this in my own projects =)

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms