You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Closure created in function, that access/updates enclosing functions parameter, does not update primitive value (int). It updates array values, it also update class variables that are primitive, so very inconsistent behavior.
Steps to reproduce
Simple script shows it (included in project, still pasting as it's real simple):
extends Node3D
func bugged(a:int = 0):
var z = func cantChange():
a = 1
z.call()
return a #this never sees updated value!
var b:int = 0
func notBugged():
var z = func cantChange():
b = 1
z.call()
return b
func notBugged2(a:Array[int] = [0]):
var z = func cantChange():
a[0] = 1
z.call()
return a[0]
# Called when the node enters the scene tree for the first time.
func _ready():
print("Here we go:")
print("Explain this: ", bugged())
print("But this works: ", notBugged())
print("But this works also: ", notBugged2())
pass # Replace with function body.
This is already mentioned in the docs, but we should make it clearer and more visible. And probably add a warning when assigning a captured local variable.
I see, behavior is extremely confusing and inconvenient (and documenting it would be super minor thing that does not do too much, since it's still annoying as heck, and also if you encounter it how you even know where to look :) I was having miscalculations in completely different place because previously this bug was triggered). But at least this behavior is understandable/justifiable since - just now reading those threads I see - gdscript does not have GC, so would be pain to deal with this for engine devs (and that is likely understatement :) ). This is unlike quite a few other decisions in gdscript :) Well, seems workarounds will need to continue.... Thanks for response!
Tested versions
System information
Godot v4.2.2.stable - openSUSE Tumbleweed 20240417 - X11 - Vulkan (Mobile) - dedicated NVIDIA GeForce RTX 4080 (nvidia) - AMD Ryzen 7 2700X Eight-Core Processor (16 Threads)
Issue description
Closure created in function, that access/updates enclosing functions parameter, does not update primitive value (int). It updates array values, it also update class variables that are primitive, so very inconsistent behavior.
Steps to reproduce
Simple script shows it (included in project, still pasting as it's real simple):
Minimal reproduction project (MRP)
godot_bug.zip
The text was updated successfully, but these errors were encountered: