Attention |
Topic was automatically imported from the old Question2Answer platform. | |

Asked By |
DuteNait |

I tried to recreate a problem where a Matrix (Array inside Array) changes it’s values even though it shouldn’t.

For some reason after setting the tilemap, the Matrix *empty cells* changes it’s values to the same as the Matrix *cells*

```
extends Node2D
const WORLDSIZE = 5
onready var tilemap = $TileMap
var cells = []
var empty_cells = []
var timer = 0
func _ready():
create_empty_matrix()
create_random_life()
set_tilemap()
print("Cells (should be random 1 and 0): ", cells)
print("Empty Cells (should be all 0): ", empty_cells) #this should still be a empty Matrix
func set_tilemap():
for x in cells.size():
for y in cells.size():
if cells[x][y] == 0:
tilemap.set_cell(x,y,0)
else:
tilemap.set_cell(x,y,1)
func create_empty_matrix():
var empty_matrix = []
for x in WORLDSIZE:
empty_matrix.append([])
for y in WORLDSIZE:
empty_matrix[x].append([])
empty_matrix[x][y] = 0
cells = empty_matrix
empty_cells = empty_matrix
print("Cells (should be all 0): ", cells)
print("Empty Cells (should be all 0): ", empty_cells)
func create_random_life():
for x in cells.size():
for y in cells.size():
if rand_range(0, 5) >= 3:
cells[x][y] = 1
else:
cells[x][y] = 0
```

With my Limited understanding *empty cells* only gets called two times and no where am I assining it the same value as *cells*.

Am I turning mad? What am I doing wrong? Can someone replicate this?