The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes
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

in Engine by (75 points)

Change grid[x[y]] to grid[x][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[x] has is array with arrays in it
grid[x][y] gets array gridx from grid and then gets array xy from array gridx
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.

1 Answer

0 votes

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[x][y][z].

by (98 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.