I have recently started trying to implement audio into my project however while attempting to create a fading effect this code causes my program to stop responding. I believe I am probably doing something wrong with the arrays as this is my first time using them in godot at least.
This code is for fading in but i also hope to use a similar method to fade out
Thanks
this code extends a node 2d with audio stream players as children fetchsong() takes an integer which corresponds to a song and sets fetched song to the audio stream player for that song for example fetchsong(5) would set fetchedsong to $intro. ‘i’ is meant to be set as the node fetched from fetchsong()
fadequeue is an array, and should be indexed with integers. i is whatever was in that cell of the array, which in this case is an AudioStreamPlayer, so you’re trying to dereference an array with something that isn’t an integer, and that’s what the error says. Arrays are integer-indexed only.
Try:
func _process(_delta: float) -> void:
if fading:
for i: int in fadequeue.size():
fadequeue[i].volume_db = lerpf(fadequeue[i].volume_db, apprachvolume[i], 0.5)
You’re going to get some kind of weird results from that, though. volume_db is logarithmic, and your fixed timevalue lerpf() is also going to be logarithmic, so your fade speed is going to be strange.
You probably want something more like:
const FADE_TIME: float = 2.0 # 2 seconds
const MIN_VOL: float = -100.0 # Decibels are logarithmic.
const MAX_VOL: float = 0.0
var fade_counter: float # Set this to 0.0 at fade start.
func _process(delta: float) -> void:
if fading:
fade_counter = clampf(fade_counter + delta, 0.0, FADE_TIME)
var fade_level: float = fade_counter / FADE_TIME
for snd: AudioStreamPlayer in fadequeue:
snd.volume_db = lerpf(0.0, -100.0, fade_level)
That will give you predictable framerate-independent fades. You might want to consider switching to linear volume as well, depending on how comfortable you are with decibels.