Use tween to change audio volume in an autoload

Godot Version

v4.2.1.stable.official [b09f793f5]


I’m trying to create a function where if the player pauses the game, the music will get quieter, and will return to what it previously was when the game is unpaused.

I’m managing my game audio in an autoload which has a variable for music volume, and a function to change the audio of a bus:

var music_volume := 1.0:
        var new_volume := clampf(value, 0.0, 1.0)
        set_bus_volume(&"Music", new_volume)

func set_bus_volume(bus: StringName, value: float) -> void:
    var bus_index := AudioServer.get_bus_index(bus)
    AudioServer.set_bus_volume_db(bus_index, linear_to_db(value))

This works well for my audio slider in my game settings.

My next step was to just lower the volume of the music when the game is paused by using a tween like so:

func _lower_volume() -> void:
    var tween := create_tween()
        music_volume - 0.5,

However this is where I run into an issue. Whenever I run the tween, music_volume always starts at 1.0, even if music_volume is modified via the audio slider.
I also tried adding .from_current() to the end of the tween_property, but no change.

Any idea why this happens and what I can do?

you don’t update this? you just make local variable var new_volume?


Doesn’t seem to change anything. For some reason when I grab music_volume and put it in a variable, it’s always at 1 even when change before calling the method.

Ok I understand what you meant by that, I didn’t set music_volume after clamping the value

var music_volume := 1.0:
		var clamped_volume := clampf(value, 0.0, 1.0)
		music_volume = clamped_volume
		set_bus_volume(&"Music", music_volume)

super sorry for missing that, my bad


remember to mark the topic as solved :innocent:


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