0 votes

I want to stop print with yield()

 func _physics_process(delta):
        example()

    func example():
        print("something")
        yield()
in Engine by (181 points)

1 Answer

+1 vote

I think you are looking for this:
yield(get_tree().create_timer(5.0), "timeout")
Where the float passed to the create_timer() function is the amount of time that will be yielded. BUT BE CAREFULL WITH THIS. If you change scenes while this is yielding, it will crash your game in release. I think they are planning on fixing this in Godot 4.0, but for now, be aware of it.

If you want to yield for one or more physics frames, I think it's best to just use a counter.

var counter = 0
var frames_to_wait = 1

func _physics_process(delta):
     if counter >= frames_to_wait:
          'do something'
     counter += 1

Finally, if these don't answer your question, check out the docs on using the yield function:
https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_basics.html#coroutines-with-yield

by (561 points)

I want to stop and print once

oh haha, just use a flag:

var has_run = false

 func _physics_process(delta):
     if !has_run:
           has_run = true
           example()

    func example():
           print("something")
            yield()
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 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.