Topic was automatically imported from the old Question2Answer platform.
Asked By
Friends-Games
So, I want to fix a problem that happens when I want to turn on an AudioStreamPlayer through GDScript. The script and the engine have no errors, but when I play it, it just keeps restarting. I know the problem, but I don’t know how to solve it.
func _process(delta):
#the problem
if Global.M3GA10VAN1A == true:
#if statement
self.play()
The problem is it loops since it’s in func _process.
But the if statement happens when the player does something specific.
So, as long as your Global.M3GA10VAN1A variable is true, your code will start the AudioStreamPlayer on each frame. And, calling play() starts the audio from its beginning. So, you’re effectively restarting the audio every frame, as long as that variable is true.
Since you didn’t specify what that variable does, or how it’s controlled, it’s hard to be specific about advice. That said, maybe all you want to do is play the audio if your variable is true and the audio isn’t already playing. If that’s the case, then this should work:
if Global.M3GA10VAN1A && !self.playing:
self.play()