How would I declare a multidimensional array?

Continuing the discussion from C# line to Lua?:

I have been trying to use multidimensional arrays and I am unsure on how I would declare it at the start in this situation
In C#, I would have just declared it like this:

float[,,] array1

array1 = new float[30, 20 ,30]

In Lua, I have tried to replicate this but i believe i am not actually creating a multidimensional array but just a single one by doing this

array1 = {30, 20, 30}

So instead I found a way to create multidimensional arrays by doing this:

local array1 = {}
for i=1,30 do
	array[i] = {}
	for j=1,20 do
		array[i][j] = {}
		for k=1,30 do
			array[i][j][k] = ?
		end
	end
end

The question mark is where I am confused though. I want to set the values like i was setting it in C# but this is only giving me one value to set.
It may just be not understanding multidimensional tables in Lua however.

1 Like

In C#, when you do array1 = new float[30, 20, 30], each item in the multi-dimensional array defaults to 0 (because it’s a float array; any number array initializes as 0).

So your question mark should just be 0.

When you want to get or set data in C#, it would look like array1[1, 5, 10] (random example), and in Lua it would look like array1[1][5][10] (albeit a bit different since Lua arrays start at 1 and C# start at 0, but you get the idea).

4 Likes