Added some guard conditionals to AudioFader to make it resilient against, for example, playing null streams.

This commit is contained in:
blujai831 2024-01-24 15:39:13 -08:00
parent 7276a4a978
commit b6b19cbd7b
No known key found for this signature in database
GPG Key ID: DDC31A0363AA5E66
1 changed files with 10 additions and 8 deletions

View File

@ -27,12 +27,13 @@ func _create_members() -> void:
## Fades in the current audio player.
func fade_in(stream: AudioStream, fade: float = DEFAULT_AUDIO_FADE) -> void:
await fade_out()
player.volume_db = AUDIO_FADE_MIN_VOLUME
player.stream = stream
player.play()
var tween := await tween_channel.create_tween(player)
tween.tween_property(player, ^'volume_db', 0.0, fade)
await tween_channel.sync(tween)
if player.stream:
player.volume_db = AUDIO_FADE_MIN_VOLUME
player.play()
var tween := await tween_channel.create_tween(player)
tween.tween_property(player, ^'volume_db', 0.0, fade)
await tween_channel.sync(tween)
## Fades out an arbitrary audio player on an arbitrary tween channel.
static func _fade_out_foreign(
@ -40,7 +41,7 @@ static func _fade_out_foreign(
p_tween_channel: TweenChannel,
fade: float = DEFAULT_AUDIO_FADE
) -> void:
if p_player.volume_db > AUDIO_FADE_MIN_VOLUME:
if p_player.playing and p_player.volume_db > AUDIO_FADE_MIN_VOLUME:
var tween := await p_tween_channel.create_tween(p_player)
tween.tween_property(
p_player, ^'volume_db', AUDIO_FADE_MIN_VOLUME, fade
@ -55,9 +56,10 @@ func fade_out(fade: float = DEFAULT_AUDIO_FADE) -> void:
## Plays audio without fading in.
func play(stream: AudioStream) -> void:
await player.fade_out()
player.volume_db = 0.0
player.stream = stream
player.play()
if player.stream:
player.volume_db = 0.0
player.play()
## Fades out old audio and fades in new audio at the same time.
func crossfade(stream: AudioStream, fade: float = DEFAULT_AUDIO_FADE) -> void: