How to declare 2d arrays / matrices in GDScript?

Godot Version

4.2.1

Question

I want to implement an adjacency matrix for a custom graph data structure. How can I declare 2d arrays / matrices in GDScript? When I try

var _adjacency_matrix: Array[Array[int]]

It says

Error at (3, 40): Nested typed collections are not supported.

Nested typed arrays aren’t supported yet. The most you can do is var _array:Array[Array]

So I suppose this is the way to go for now? Are there plans to add support for nested typed arrays in the future?

var _adjacency_matrix: Array[Array]

func setup(p_size: int) -> void:
	size = p_size
	
	for i in range(size):
		_adjacency_matrix.append([]) 
		for j in range(size):
			_adjacency_matrix[i].append(j)


func populate() -> void:
	var n = 0
	for i in range(size):
		for j in range(size):
			n += 1
			var vertex := Vertex.new()
			vertex.label = str(n)
			
			_adjacency_matrix[i][j] = vertex
2 Likes