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 =)