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

"Array Array(from: Array)" constructor doesn't make a copy, against what the documentation says. #56389

Closed
cdemirer opened this issue Dec 31, 2021 · 3 comments · Fixed by #69248

Comments

@cdemirer
Copy link
Contributor

Godot version

4.0.dev (28174d5)

System information

5.15.2-2-MANJARO

Issue description

Array Array(from: Array)
Constructs an Array as a copy of the given Array.

However, that is not the case.

Steps to reproduce

func _ready():
	var x = [1, 2, 3]
	var y = Array(x)
	x.append(4)
	print(x)
	print(y)
[1, 2, 3, 4]
[1, 2, 3, 4]

Minimal reproduction project

No response

@Calinou Calinou added the bug label Dec 31, 2021
@Calinou
Copy link
Member

Calinou commented Dec 31, 2021

I can confirm this on both master 28174d5 and 3.x a75afd6, so this is not a regression in master.

@MarianoGnu
Copy link
Contributor

MarianoGnu commented Dec 1, 2022

I can confirm it also happens with Dictionary, and i assume it happens with all other variants of PackedXXXArray
EDIT NOTE: still happening on godot 4.0 beta 6

func _ready() -> void:
	var dic = {
		c = randi(),
		d = randi()
	}
	var dic2 = Dictionary(dic)
	print(dic, dic2)
	__(dic)
	print(dic, dic2)
	breakpoint

	var arr = [1, 2, 3]
	var arr2 = Array(arr)
	print(arr, arr2)
	___(arr2)
	print(arr, arr2)
	breakpoint

func ___(arg):
	arg.push_back(randi())
	arg.push_back(randi())

func __(arg):
	arg["a"] = randi()
	arg["b"] = randi()
{ "c": 424301647, "d": 2408057864 }{ "c": 424301647, "d": 2408057864 }
{ "c": 424301647, "d": 2408057864, "a": 124132984, "b": 298769218 }{ "c": 424301647, "d": 2408057864, "a": 124132984, "b": 298769218 }
[1, 2, 3][1, 2, 3]
[1, 2, 3, 3409322164, 472855835][1, 2, 3, 3409322164, 472855835]

@vonagam
Copy link
Contributor

vonagam commented Jan 17, 2023

The documentation is wrong. Correct one is in Dictionary for some reason:

Returns the same array as [param from]. If you need a copy of the array, use [method duplicate].

The method Array(array) is useless much for gdscript, but you cannot simply change that constructor in core codebase to make a copy because it is used in some parts of codebase to get variant for an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants