How to add a third layer of nested array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Drachenbauer
for x in range(grid_size.x):
    target_grid.append([])
    grid.append([])
    for y in range(grid_size.y):
        target_grid[x].append([])
        grid[x].append([])
        for _z in range(grid_size.z):
            target_grid[x[y]].append(null)
            grid[x[y]].append(null)

I try to put another layer of nested layers for the z-axis.
but in the lines inside the z-for-loop i got the error:

Can`t index on a value of type “int”

What do i wrong

Change grid[x[y]] to grid[comment0-y].

Why you need to do this:
grid is an array
x is an index in the array which could be another array but is most likely an int
grid has is array with arrays in it
grid[comment0-y] gets array grid_x from grid and then gets array x_y from array grid_x
grid[x[y]] get array x_y from x which is then used as an index for grid which won’t work unless x is an array which according to your for loop it is a int so indexing will fail.

codelyok13 | 2022-03-22 18:13

:bust_in_silhouette: Reply From: codelyok13

If your willing to make changes, I recommend using Vector3 with a 1D Dictionary to simulate a 3D array. Else, skip to the bottom on how to fix your issue.

var dict = {}
var first = Vector3(0,0,0)
dict[first] = whatever
var second = Vector3(2,43,1)
dict[second] = whatever2

I would rewrite your grid so that it is easier to use

var grid = {}

func get_item(x,y,z):
   var index = Vector3(x,y,z)
   if index in grid:
      return grid[ index ]
   return null #or whatever you want

func set_item(x,y,z, data):
   var index = Vector3(x,y,z)
   grid[ index ] = data

func fill_gird(max_x, max_y, max_z, filler = null):
   #loop for x
      #loop for y
         for z in range(0, max_z): #loop for z
            var index = Vector3(x,y,z)
            grid[index] = filler

How to fix:

for x in range(grid_size.x):
    target_grid.append([])
    grid.append([])
    for y in range(grid_size.y):
        target_grid[x].append([])
        grid[x].append([])
        for _z in range(grid_size.z):
            target_grid[x][y].append(null)
            grid[x][y].append(null)

Access target grid using grid[y][z].