EDIT / WARNING: I updated the example to clean up the SceneTreeTimer, as it will still be counting and call
resume() twice. This fixes an error, but adds some distracting complexity to the code. The timer code can be ignored, its only to serve the example. If timers with yields are needed, I recommend using your own timers to have better control over them.
In addition to what you're doing, if you need to cancel a yield, you will want to keep track of the
GDFunctionState that the
yield() returns. https://docs.huihoo.com/godotengine/godot-docs/godot/classes/class_gdfunctionstate.html
A yield will return the function immediately with that object, and you can hold on to it and use it as you like.
First check if it is still
is_valid(), and then call
resume() to cancel the yield.
Additionally, you want to exit the function that is doing the yielding. To do this you can pass optional arguments into the
resume(args), and capture them in
var and then use them in a following
if statement like you have above.
In your case, it is just
Here I have an example of 5 second timer, where I'm using a button to do the cancel.
The flow can be confusing, since things are jumping around.
const CANCEL = true
pending_yield = do_stuff()
# This what comes back out of the return from a yielded function.
# Resume can pass a value back.
# Cleans up SceneTreeTimer
# Otherwise it will call resume() too.
timer = null
timer = get_tree().create_timer(5.0) # Keep reference to timer.
var result = yield(timer, "timeout")
if(result and result == CANCEL):
print("The yield was cancelled. Stopping the function.")
print("Waited 5 seconds... carrying on.")
timer = null