Lambdas capture local variables by value

In Godot, GDScript lambdas only copy the value of each variable it captures (by design; see godotengine/godot#69014). However, since the value of a Variant is itself a reference, you can use a Variant to get around this problem if you want the lambda to mutate a capture.

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

I used to consider this a Godot crime, but I have since changed my mind.

History