Godot scripts execute before my game runs! How do I stop it?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Adham
:warning: Old Version Published before Godot 3 was released.

So say I have the script (‘button’ is a button control in my scene)

func _ready():
    var test_value = test()

    print('you should only see this after test() fully finished')
    #this means test_value should now be 'DONE'

    #however 'DONE' is never printed! Instead, I get '[GDFunctionState:546]'

func test():
    print('started test')
    yield(button, 'pressed')
    print('finished test')
    return 'DONE'

How do I stop this kind of asynchronous behaviour? Reminds me of JS promises except it isn’t.

I also tried doing while test_value extends GDFunctionState: continue to try and stall execution until I get my test_value…however, doing this just hangs my game, I only see the Godot logo and it keeps loading forever :(.

Any help is greatly appreciated, I’ve wasted tons of time on this & still can’t solve it.

:bust_in_silhouette: Reply From: vinod

Your test function turns to a coroutine if you use a yield statement inside it. So when you call the test function, Godot runs the test function code until it hits the yield statement. At this time a coroutine manager will stop the execution of the function and save the state of the function until the yield is qualified.
Resuming takes sometime depending on the signal used in the yield. At the time yield is qualified, the coroutine manager will run the remaining code in the function inside it’s own scope.

Always remember that yield is function scoped. When the engine hits a yield, the function execution is actually complete. So it returns and runs the next line in the code.

You have to call the remaining things from the test function itself after the yield.

I guess I’ll just copy the body of the test function & paste it into _ready(). This is the model I had in the start. However, I was trying to make my code more modular & clean.

Is there a way to encapsulate blocks of code that execute to the end (& include parts that wait for input there)? Or do I have to spaghetti code through this? Thanks btw!

Adham | 2016-07-20 08:13

You can make those sections in another function and call that function after yield.

vinod | 2016-07-20 08:31