How can I make a 2D array with Lua? I need to dynamically create this.
local tbl = { { } }
Something like the above but where I can specify how many items. In my case they'll be the same amount. I basically want to access it like tbl[3][5].
Thanks
To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.
Yes, the arrays in Lua start with index 1 as the first index and not index 0 as you might have seen in most of the programming languages.
-- Create a 3 x 5 array
grid = {}
for i = 1, 3 do
grid[i] = {}
for j = 1, 5 do
grid[i][j] = 0 -- Fill the values here
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With