I play a sound and its continuously playing from start every tick

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AntBagel

so I have this horror game which I want to play vent sounds(like something is crawling in the vent) when you do something incorrectly. however when i try to do this then its playing the start every tick. I think the problem is that I used _process but idk what to change it to.
this is the code:

func _process(delta):
if GlobalPoints.badpoints >= 1 and GlobalPoints.badpoints < 4:
playmusic()
elif GlobalPoints.badpoints == 4:
playmusicfast()

func playmusic():
AudioPlayer.play()

func playmusicfast():
AudioPlayer2.play()

:bust_in_silhouette: Reply From: jgodfrey

So, I assume you just want to play the sound if it’s not already playing, right?

In that case, something like this (using the AudioPlayer reference you show above):

func playmusic():
    if !AudioPlayer.playing:
        AudioPlayer.play()

So, check if it’s already playing. If so, don’t restart it. If not, start it. And, a similar change to the other method…

thanks oyu very much it worked!

AntBagel | 2022-04-10 06:26