Multi_dimensional arrays

ok, wondering if there was anyway at all to allow Godot to build multi-dimensional Arrays in a future build? I know how to use GDscript code to create one, but would be nice if it was built in.

thanks for your time.

1 Like

You can already nest arrays ( Array[Array], or just [[1, 2], [3, 4]]). There’s no separate multi-dim array type though, and nested typed arrays like Array[Array[int]] aren’t supported yet as far as i know.

1 Like

I think I agree with you @Moronicmisfit, it would be nice if it was built in. Not sure how much use it would get but it would get some.

var my_3d_array: Array[x:int][y:int][z:int]

I effectively implement that when I need to via dictionary or nested array, though coming from a C background I too would like to see something like that.

You can write your own stride-based multidimensional array type. In the long term, that would be the best approach. Some significant up-front effort, but you get an actual multidimensional array, not a cheap imitation.

Failing that, an array of arrays or a dictionary with vectors as keys would also work.

They know how to do it, they were suggesting it would be nice if it was built in so memory blocks for them were contigous.

I think this was a valid comment. If anyone posts this in Godot GDScript suggestions I will support it.

1 Like

If by extension, you would use it so:
my_3d_array[0][0][0] = 12
Then this format would be a major breaking change since currently this is valid syntax.

	var my_3d_array:Array = [[1,2,[3,4]]]
	print(my_3d_array[0][2][0])

So you would need to find another syntax for it.
It is probably better to have separate variable type for it.

3 Likes

My preferred syntax for multidimensional array access is to use a vector type:

var a: Array3D[int] = function_that_creates_3d_array()
var idx: = Vector3i(0, 0, 0)
a[idx] = 5
1 Like

There’s already stuff to :+1: here:

1 Like

Just out of curiosity: what would you use it for?

For myself I am doing a procedurally generated simulated environment in my 2D game. I might use them to store my x-component (where we are in the world), the thorn density, the mood, the light levels, the creature generation, the plant generation… anything and everything really. With a multi dimensional array I could do [chunk_position, magical intensity, corruption] but more importantly it would be easily reversible.

When I tried to do a 3D game of life, it would have been great for that too.

Yes, I am managing fine without it. But like the OP said, it would be nice if it was built in.

How handy would that be. My creature procedural generation would be a cinch with this especially varying it with time of day.

@japonbaligi
Thanks for those links. Support given!

2 Likes

to BABA, for everything that involves x , y and or z arrays. as in a table of X,Y,Z positions for 3D. lots of other uses for multi-dimensional arrays. most high level programming languages have multi-dimensional arrays. And its makes programming life better. Been programming since the mid 80s, used these kind of arrays a lot. and yes as stated, I can create my own , in code, by nesting arrays with-in arrays. but its a pain and be better if it were built-in . Thanks for your time. and happy coding.

1 Like

Thanks for your replies both of you! Lets hope they have added them until i start a 3d project sometime in future​:grinning_face_with_smiling_eyes:

Another possibility is to make multidimensional array into one.

a b c d
e f g h
i j k l
a b c d e f g h i j k l

If I have a knowledge for the dimension of my 2d array (here width = 4 height = 3)
I can get coord from an index and an index from coord.

func get_index_from_coord(coord:Vector2I):
  return coord.x+dimension.x*coord.y

here g is (2,1) : computation … 2 +4*1 = 6
coord (2,1) is the 6th slot of my array.