0 votes

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.

in Engine by (99 points)

2 Answers

+1 vote
Best answer

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()
by (12,938 points)
selected by
0 votes

just do this:

func _process(delta):

if Global.M3GA10VAN1A == true:
$AudioStreamPlayer.play()

by (213 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.