Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Roblox 2D int array to store information?

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

✓ Accepted answer

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.

1 comment

0

Mathematically and logically this makes seance because i do the same with my terrain generating script. Thank you for explaining how this worked.

This discussion is preserved as it was. New answers, comments, and votes are closed.