Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure fails to update primitive parameter #93070

Closed
morphles opened this issue Jun 12, 2024 · 2 comments · Fixed by godotengine/godot-docs#9556 or #93691
Closed

Closure fails to update primitive parameter #93070

morphles opened this issue Jun 12, 2024 · 2 comments · Fixed by godotengine/godot-docs#9556 or #93691

Comments

@morphles
Copy link

morphles commented Jun 12, 2024

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):

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.

Minimal reproduction project (MRP)

godot_bug.zip

@dalexeev
Copy link
Member

dalexeev commented Jun 12, 2024

This is not a bug, but an limitation. See the following issues for details:

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.

@morphles
Copy link
Author

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!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
3 participants