I stumbled upon a weird issue when playing sound using AudioPlayer node in my root scene when trying to run it in the _process(delta) function in a script connected to an AnimatedSprite node. The sound used is very short and only plays the sound for one step basically so I made my code to simply reflect this in the script as the following:
extends AnimatedSprite
func load_sound(sound):
get_node("/root/AudioPlayer").load_sound(sound)
func start(delta, animation_type):
animation = animation_type
playing = true
func _process(delta):
if(playing):
if(get_frame() % 2 == 0):
get_node("/root/AudioPlayer").play_sound()
func stop():
playing = false
Firstly, I load the sound in my player script and then whenever the animation is being triggered by having the mouse click to a different location, I set playing the animation to true.
Then the animation plays (works all fine so far), then during the play of the animation I use _process(delta) function to yield the current frame and as can be seen, anytime the frame count is at 2 play the sound. This does indeed work fine. The sound sounds step like and works anytime the animation is playing. The timing is just fine as well and sounds authentic. The only thing that bothers me is that somehow there is some glitchy / distorted noise when playing the sound. When playing the sound outside of the _process(delta) function in the script, this glitch / distorted noise is nonexistant. So my question here is, why does this noise occur? How can I fix this?