Gdscript Bug or strange behaviour

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

I’ve got a global script to handle the game data.

in there are two variables with their getters and setters

var gameEntry = [] setget setGameEntry, getGameEntry
var gameEntries = [] setget setGameEntries, getGameEntries

Every time gameEntry is changed it will be appended to the gameEntries array.

But the strange thing is that when I add gameEntry to gameEntries and change gameEntry the gameEntry in gameEntries also changes like they are linked.

gameEntry = [0, 0, 0]

Add gameEntry to gameEntries


func setGameEntries(entry):

gameEntries = [[0, 0, 0]]

change gameEntry to [1, 0, 1]

gameEntries = [[1, 0, 1]] // ??

expected behaviour:

gameEntries = [[0, 0, 0]]

How can i add the value of an array to another array without them being linked

:bust_in_silhouette: Reply From: jgodfrey

An array is just a reference to a location in memory. So, no matter how many places you store that reference, it still points to the same array, and changes in one stored reference will be reflected in the others.

To change that, you need to make a copy of the array, which can be done via Array.duplicate().