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:
But this does:
I used to consider this a Godot crime, but I have since changed my mind.