Create a 3D integer array

Godot Version

4.2

Question

can anyone share a func(sizeX,sizeY,sizeZ) that creates a 3d array and fills it with an int of zero?

I assume you want a nested array in GDScript?

func empty_array_3(size_x: int, size_y: int, size_z: int, contents = 0) -> Array:
	var inner: = []
	inner.resize(size_z)
	inner.fill(contents)
	
	var mid: = []
	mid.resize(size_y)
	for y in size_y:
		mid[y] = inner.duplicate()
	
	var outer: = []
	outer.resize(size_x)
	for x in size_x:
		outer[x] = mid.duplicate()
	return outer

This is an array you access by arr[x_index][y_index][z_index]

which is nice when you want the order of the indexes to be x, y, z, but if you want the structure to look like this:

 [ 
  [(x0 y0 z0),  (x1 y0 z0),  (x2 y0 z0)],
  [(x0 y1 z0),  (x1 y1 z0),  (x2 y1 z0)]
 ],
 [   
  [(x0 y0 z1),  (x1 y0 z1),  (x2 y0 z1)],
  [(x0 y1 z1),  (x1 y1 z1),  (x2 y1 z1)]
 ]

then you have to reverse the order of the indices though, like arr[z_index][y_index][x_index]

3 Likes

Excellent . This works great! Thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.