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. 1 2
For example, this does not work: 2
But this does: 2
I used to consider this a Godot crime, but I have since changed my mind. 3 2