Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Roblox 2D int array to store information?

Asked by 8 years ago

I am wondering how you would go about making a array with two indexes. I am used to using int[,] for C# but in Lua i do not know how i can accomplish this?

I am using this to store information in a [x,y] grid array were i set a value like 1 or 5. This is to store information about a 2D terrain when generating for placing objects. I do not know if that helps with the question.

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

In Lua you can just make a table where each index is another table.

local array = {}
for i = 1, 10 do
    local new = {}
    array[i] = new
    for j = 1, 10 do
        new[j] = (i-1)*10+j
    end
end

If you want to set a value of some index higher than 10 in this case, you need to add another table first, e.g.

array[27] = {}
array[27][16] = 5

accessing values is no surprise

print(array[5][2])

52

All a multidimensional array in Lua is is a table that has other tables inside of it.

0
Mathematically and logically this makes seance because i do the same with my terrain generating script. Thank you for explaining how this worked. SciGuy2002 15 — 8y
Ad

Answer this question