-
What do you want to achieve?
I was trying to create a function that returns a 3 dimensional array.
This is the code:
function three_dimensional_array(x:number, y:number, z:number)
local axis_table = {[0] = 0}
for ai, axis in pairs({x, y, z}) do
axis_table[ai] = {}
for i = 1, axis do
table.insert(axis_table[ai], axis_table[ai-1])
end
end
return axis_table[3]
end
-
What is the issue?
Whenever I change a value inside the array this happens
a_table = three_dimensional_array(3, 3, 3)
a_table[1][1][1] = "hi"
print(a_table)
Output:
{
[1] = {
[1] = {
[1] = "hi",
[2] = 0,
[3] = 0
},
[2] = {
[1] = "hi",
[2] = 0,
[3] = 0
},
[3] = {
[1] = "hi",
[2] = 0,
[3] = 0
}
},
[2] = {
[1] = {
[1] = "hi",
[2] = 0,
[3] = 0
},
[2] = {
[1] = "hi",
[2] = 0,
[3] = 0
},
[3] = {
[1] = "hi",
[2] = 0,
[3] = 0
}
},
[3] = {
[1] = {
[1] = "hi",
[2] = 0,
[3] = 0
},
[2] = {
[1] = "hi",
[2] = 0,
[3] = 0
},
[3] = {
[1] = "hi",
[2] = 0,
[3] = 0
}
}
}
As you can see the “hi” repeated because the tables are synchronizing.
-
What solutions have you tried so far?
I tried unpacking the table and {} it together but it doesn’t work for tables inside of tables