Synchronous method call

lomaloma Posts: 2Member
edited September 14 in Programming

I am a GDScript beginner. Here's my scenario:

func m1():
    if(test):
        yield(animate(), 'completed')

func m2():
    m1()
    print('done')

I want print to be called after m1 is finished. But if I put m1 call in yield, I get error if condition test is not fulfilled.

What's the best approach for such situations.


Tags :

Answers

  • fire7sidefire7side Posts: 233Member

    Can't you do something like this:

    var test = true
    
    func _ready():
        if(test):
            try_timer()
    
    func try_timer():
        $Timer.start()
        yield($Timer,"timeout")
        print("finished")
    
    
  • lomaloma Posts: 2Member

    @fire7side Thank you for your response. I don't think this your approach would address my problem.
    Simply put the problem is: if I want to call a method that may or may not do asynchronous tasks like tween.start(), how do I wait for it's completion. I am assuming, you wait by making a call to yield(fn, 'completed').

  • fire7sidefire7side Posts: 233Member
    edited September 15

    I'm pretty sure tween gives a completed signal, and that's how you wait for it, but when you have a branch like that with a yield that may or may not be called so the function might wait or not, I'm not sure the compiler will like that. I haven't really done it though. I always call a function that has the yield in it that does the waiting in the branch instead. That's what the example was for. It may work though, I just don't do it that way.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file