How would I append a 2d array to a 3d array without using a reference?

Godot Version



As a beginner project, I decided to make chess. I have a 2d array (named boardState) that represents the state of the board and the location of the pieces, which changes every turn. I want to append it to another array (creating a 3d array) at the end of every turn to keep track of what the board has looked like throughout the game. This would allow me to detect threefold repetition and go back to look at previous moves after the game has ended.

The issue I’m having is that whenever boardState is changed, it changes all iterations of it that have been added to the 3d array, which entirely defeats its purpose since it can no longer record previous boardStates.

I assume this is because 3dArray.append(boardState) appends a reference to boardState instead of its contents, and thus all values of the 3d array simply become references to boardState. Is there a way to avoid this?

Use the arrays duplicate() method.
This makes a copy of the array not a reference.