Found another Godot crime: https://github.com/godotengine/godot/issues/69014
It looks like lambdas do not capture local variables as they only copy the value of each variable. However, since Variant
is passed by reference, you can use a Variant
to get around this problem.
For example, this does not work:
func foobar():
var stop = false
var stop_callback = func():
stop = true
some_signal.connect(stop_callback)
await get_tree().create_timer(10).timeout
print(stop) # Always false
But this does:
func foobar():
var stop = { "value": false }
var stop_callback = func():
stop["value"] = true
some_signal.connect(stop_callback)
await get_tree().create_timer(10).timeout
print(stop["value"]) # Either true or false