Found a bug bug (maybe)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MOSN

If I define an array as equal to another array, then any function I use on the second array, also affects the first one. Example:

var real_array = [1, 2, 3]

func _ready():
var temp_array = real_array
temp_array.remove(0)
print(real_array) —> print [2, 3]

Now I found the duplicate() function while writing this and it makes me think that this might not be a bug after all, but it does seem like odd behavior since other types of variables don’t function like this (at least the ones I’ve tested).

I think this should be an issue on GitHub.

MmTtDeveloper | 2019-04-20 18:52

:bust_in_silhouette: Reply From: wombatstampede

The assignment only copies a reference to the original structure. This is also true for all objects and dictionaries and is by design. Use the duplicate() method to assign a duplicate of the array.

Fair enough. Though I can’t see why it would ever be useful. But yea had figured out duplicate() was an option.

MOSN | 2019-04-21 09:14

This is quite common, also in other languages (i.e. JavaScript). Normally, copying complex data structures can take quite a bit of time (& sometimes memory) so the default behaviour for all complex datatypes is just assigning by reference.

wombatstampede | 2019-04-22 09:41

Fair enough. Thanks for the info.

MOSN | 2019-04-22 20:24