#Use tween to change audio volume in autoload

2 messages · Page 1 of 1 (latest)

manic fjord
#

Hi, 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:
    set(value):
        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()
    tween.tween_property(
        self,
        ^"music_volume",
        music_volume - 0.5,
        _VOLUME_CHANGE_DURATION
    )

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. Any idea why this happens and what I can do?

Using Godot 4.2.1.

#

Also I just tried adding .from_current() to the end of the tween_property, but no change.