Using “Erase” Method on Array X Is Affecting Elements From Array Y

Godot Version

Godot Engine v4.3

Question

Hi all, recently discovered something about GDScript and wondering if I’m doing something wrong or if this is a bug/intended feature. Here’s some code I wrote to demonstrate the problem:

var x: Array = range(0, 10)
var y: Array = x
y.erase(5)
print("Array X: ", x)
print("Array Y: ", y)

As you can see, I have declared two separate arrays, X and Y. The value of X is initialised to the range of integers between 0 to 10 (not including 10), and the value of Y is initialised to be equal to X – so far so good, right? However on the 3rd line we use the erase method to remove the integer “5” from Y. We should therefore expect that X will remain unchanged, and that Y will be equal to [0, 1, 2, 3, 4, 6, 7, 8, 9], but my output demonstrates that this is not what happens:

Array X: [0, 1, 2, 3, 4, 6, 7, 8, 9]
Array Y: [0, 1, 2, 3, 4, 6, 7, 8, 9]

Why is the erase method affecting X when Y is merely a copy? Is there something I’m not understanding here?

2 Likes

Yup, thanks a bunch.