好的我有这张桌子:
tbm.offsets = { -- facedir indexed (+1)
{ -- facedir = 0
{ -- first line
{ X = -1, Y = 2, Z = 1 },
{ X = 0, Y = 2, Z = 1 },
{ X = 1, Y = 2, Z = 1 }
},
{ -- second line
{ X = -1, Y = 1, Z = 1 },
{ X = 0, Y = 1, Z = 1 },
{ X = 1, Y = 1, Z = 1 }
},
{ -- third line
{ X = -1, Y = 0, Z = 1 },
{ X = 0, Y = 0, Z = 1 },
{ X = 1, Y = 0, Z = 1 }
},
{ -- forth line
{ X = -1, Y = -1, Z = 1 },
{ X = 0, Y = -1, Z = 1 },
{ X = 1, Y = -1, Z = 1 }
}
},
{ -- facedir = 1
{ -- first line
{ X = 1, Y = 2, Z = -1 },
{ X = 1, Y = 2, Z = 0 },
{ X = 1, Y = 2, Z = 1 }
},
{ -- second line
{ X = 1, Y = 1, Z = -1 },
{ X = 1, Y = 1, Z = 0 },
{ X = 1, Y = 1, Z = 1 }
},
{ -- third line
{ X = 1, Y = 0, Z = -1 },
{ X = 1, Y = 0, Z = 0 },
{ X = 1, Y = 0, Z = 1 }
},
{ -- forth line
{ X = 1, Y = -1, Z = -1 },
{ X = 1, Y = -1, Z = 0 },
{ X = 1, Y = -1, Z = 1 }
}
},
{ -- facedir = 2
{ -- first line
{ X = -1, Y = 2, Z = -1 },
{ X = 0, Y = 2, Z = -1 },
{ X = 1, Y = 2, Z = -1 }
},
{ -- second line
{ X = -1, Y = 1, Z = -1 },
{ X = 0, Y = 1, Z = -1 },
{ X = 1, Y = 1, Z = -1 }
},
{ -- third line
{ X = -1, Y = 0, Z = -1 },
{ X = 0, Y = 0, Z = -1 },
{ X = 1, Y = 0, Z = -1 }
},
{ -- forth line
{ X = -1, Y = -1, Z = -1 },
{ X = 0, Y = -1, Z = -1 },
{ X = 1, Y = -1, Z = -1 }
}
},
{ -- facedir = 3
{ -- first line
{ X = -1, Y = 2, Z = -1 },
{ X = -1, Y = 2, Z = 0 },
{ X = -1, Y = 2, Z = 1 }
},
{ -- second line
{ X = -1, Y = 1, Z = -1 },
{ X = -1, Y = 1, Z = 0 },
{ X = -1, Y = 1, Z = 1 }
},
{ -- third line
{ X = -1, Y = 0, Z = -1 },
{ X = -1, Y = 0, Z = 0 },
{ X = -1, Y = 0, Z = 1 }
},
{ -- forth line
{ X = -1, Y = -1, Z = -1 },
{ X = -1, Y = -1, Z = 0 },
{ X = -1, Y = -1, Z = 1 }
}
}
}
它已经是Lua中的一个静态表但是我想让它变得动态,表格列出了多行块的XYZ坐标,我希望能够根据用户输入调整坐标表,例如他们想要一个20块3格高的网格如何从for循环中组装表格?每个块都有一个xyz并且在一行中,每一行都是一个块的一部分,坐标取决于方向面向哪个方向。
我在过去的15分钟内开始弄清楚它会是什么样的:
temp = {}
temp1 = {}
temp2 = {}
temp3 = {}
for facedir = 1, 4 do
for block = 1, 5 do
for Line = 1,5 do
for X1 = -5, 5 do
for Y1 = 5,-1 do
for Z1 = 1, 5 do
temp = {X = X1, Y = Y1, Z = Z1}
end
end
end
temp1{ line = temp }
end
temp2{block = temp1}
end
temp3{facedir = temp2}
end
上面的代码会起作用吗?或者什么是更好/更容易实现的解决方案,这个代码将重复执行,有时候有相同的变量,有时会有不同的变量。
静态样本表格在控制台"
中使用lua打印脚本格式化1 table: 0x95efdc8
1 table: 0x95efe18
1 table: 0x95f1050
X - -1
Y - 2
Z - 1
2 table: 0x95f10d0
X - 0
Y - 2
Z - 1
3 table: 0x95efb28
X - 1
Y - 2
Z - 1
2 table: 0x95efba8
1 table: 0x95efbf0
X - -1
Y - 1
Z - 1
2 table: 0x95f1658
X - 0
Y - 1
Z - 1
3 table: 0x95f16d8
X - 1
Y - 1
Z - 1
3 table: 0x95f1758
1 table: 0x95f1780
X - -1
Y - 0
Z - 1
2 table: 0x95f1800
X - 0
Y - 0
Z - 1
3 table: 0x95f1880
X - 1
Y - 0
Z - 1
4 table: 0x95f1900
1 table: 0x95f1948
X - -1
Y - -1
Z - 1
2 table: 0x95f19c8
X - 0
Y - -1
Z - 1
3 table: 0x95f1a48
X - 1
Y - -1
Z - 1
2 table: 0x95f1ac8
1 table: 0x95f1b18
1 table: 0x95f1b60
X - 1
Y - 2
Z - -1
2 table: 0x95f2308
X - 1
Y - 2
Z - 0
3 table: 0x95f2388
X - 1
Y - 2
Z - 1
2 table: 0x95f2408
1 table: 0x95f2430
X - 1
Y - 1
Z - -1
2 table: 0x95f24b0
X - 1
Y - 1
Z - 0
3 table: 0x95f2530
X - 1
Y - 1
Z - 1
3 table: 0x95f25b0
1 table: 0x95f25f8
X - 1
Y - 0
Z - -1
2 table: 0x95f2678
X - 1
Y - 0
Z - 0
3 table: 0x95f26f8
X - 1
Y - 0
Z - 1
4 table: 0x95f2778
1 table: 0x95f27c0
X - 1
Y - -1
Z - -1
2 table: 0x95f2840
X - 1
Y - -1
Z - 0
3 table: 0x95f28c0
X - 1
Y - -1
Z - 1
3 table: 0x95f2940
1 table: 0x95f2990
1 table: 0x95f29d8
X - -1
Y - 2
Z - -1
2 table: 0x95f2a58
X - 0
Y - 2
Z - -1
3 table: 0x95f2ad8
X - 1
Y - 2
Z - -1
2 table: 0x95f2b58
1 table: 0x95f2ba0
X - -1
Y - 1
Z - -1
2 table: 0x95f2c20
X - 0
Y - 1
Z - -1
3 table: 0x95f2ca0
X - 1
Y - 1
Z - -1
3 table: 0x95f2d20
1 table: 0x95f2d68
X - -1
Y - 0
Z - -1
2 table: 0x95f2de8
X - 0
Y - 0
Z - -1
3 table: 0x95f2e68
X - 1
Y - 0
Z - -1
4 table: 0x95f2ee8
1 table: 0x95f2f30
X - -1
Y - -1
Z - -1
2 table: 0x95f2fb0
X - 0
Y - -1
Z - -1
3 table: 0x95f3030
X - 1
Y - -1
Z - -1
4 table: 0x95f30b0
1 table: 0x95f3100
1 table: 0x95f3148
X - -1
Y - 2
Z - -1
2 table: 0x95f31c8
X - -1
Y - 2
Z - 0
3 table: 0x95f3248
X - -1
Y - 2
Z - 1
2 table: 0x95f32c8
1 table: 0x95f3310
X - -1
Y - 1
Z - -1
2 table: 0x95f3390
X - -1
Y - 1
Z - 0
3 table: 0x95f3410
X - -1
Y - 1
Z - 1
3 table: 0x95f3490
1 table: 0x95f34d8
X - -1
Y - 0
Z - -1
2 table: 0x95f3558
X - -1
Y - 0
Z - 0
3 table: 0x95f35d8
X - -1
Y - 0
Z - 1
4 table: 0x95f3658
1 table: 0x95f36a0
X - -1
Y - -1
Z - -1
2 table: 0x95f3720
X - -1
Y - -1
Z - 0
3 table: 0x95f37a0
X - -1
Y - -1
Z - 1
当前脚本的输出:
1 table: 0x95f3848
1 table: 0x95f3870
1 table: 0x95f3898
X - -1
Y - 2
Z - 1
2 table: 0x95f3918
X - -1
Y - 1
Z - 1
3 table: 0x95f3998
X - -1
Y - 0
Z - 1
4 table: 0x95f3a40
X - -1
Y - -1
Z - 1
5 table: 0x95f3ac0
X - 0
Y - 2
Z - 1
6 table: 0x95f3a18
X - 0
Y - 1
Z - 1
7 table: 0x95f3be0
X - 0
Y - 0
Z - 1
8 table: 0x95f3c60
X - 0
Y - -1
Z - 1
9 table: 0x95f3ce0
X - 1
Y - 2
Z - 1
10 table: 0x95f3b40
X - 1
Y - 1
Z - 1
11 table: 0x95f3e40
X - 1
Y - 0
Z - 1
12 table: 0x95f3ec0
X - 1
Y - -1
Z - 1
2 table: 0x95f3f40
1 table: 0x95f3f68
X - -1
Y - 2
Z - 1
2 table: 0x95f3fe8
X - -1
Y - 1
Z - 1
3 table: 0x95f4068
X - -1
Y - 0
Z - 1
4 table: 0x95f4110
X - -1
Y - -1
Z - 1
5 table: 0x95f4190
X - 0
Y - 2
Z - 1
6 table: 0x95f40e8
X - 0
Y - 1
Z - 1
7 table: 0x95f42b0
X - 0
Y - 0
Z - 1
8 table: 0x95f4330
X - 0
Y - -1
Z - 1
9 table: 0x95f43b0
X - 1
Y - 2
Z - 1
10 table: 0x95f4210
X - 1
Y - 1
Z - 1
11 table: 0x95eff98
X - 1
Y - 0
Z - 1
12 table: 0x95f4568
X - 1
Y - -1
Z - 1
2 table: 0x95f45e8
1 table: 0x95f4610
1 table: 0x95f4638
X - -1
Y - 2
Z - 1
2 table: 0x95f46b8
X - -1
Y - 1
Z - 1
3 table: 0x95f4738
X - -1
Y - 0
Z - 1
4 table: 0x95f47e0
X - -1
Y - -1
Z - 1
5 table: 0x95f4860
X - 0
Y - 2
Z - 1
6 table: 0x95f47b8
X - 0
Y - 1
Z - 1
7 table: 0x95f4980
X - 0
Y - 0
Z - 1
8 table: 0x95f4a00
X - 0
Y - -1
Z - 1
9 table: 0x95f4a80
X - 1
Y - 2
Z - 1
10 table: 0x95f48e0
X - 1
Y - 1
Z - 1
11 table: 0x95f4be0
X - 1
Y - 0
Z - 1
12 table: 0x95efa88
X - 1
Y - -1
Z - 1
2 table: 0x95efcf0
1 table: 0x95efe98
X - -1
Y - 2
Z - 1
2 table: 0x95f0528
X - -1
Y - 1
Z - 1
3 table: 0x95f0ec8
X - -1
Y - 0
Z - 1
4 table: 0x95f1c28
X - -1
Y - -1
Z - 1
5 table: 0x95f1ca8
X - 0
Y - 2
Z - 1
6 table: 0x95f1c00
X - 0
Y - 1
Z - 1
7 table: 0x95f1dc8
X - 0
Y - 0
Z - 1
8 table: 0x95f0788
X - 0
Y - -1
Z - 1
9 table: 0x95f0808
X - 1
Y - 2
Z - 1
10 table: 0x95f1df0
X - 1
Y - 1
Z - 1
11 table: 0x95f0968
X - 1
Y - 0
Z - 1
12 table: 0x95f09e8
X - 1
Y - -1
Z - 1
在脚本的输出中有12个条目,并且应该只有3个,所以即时播放...
答案 0 :(得分:1)
修改为语法和逻辑上正确的上述代码如下所示:
local temp3 = {}
for facedir = 1, 4 do
local temp2 = {}
for block = 1, 5 do
local temp1 = {}
for Line = 1,5 do
local temp = {}
for X1 = -5, 5 do
for Y1 = 5,-1,-1 do
for Z1 = 1, 5 do
temp[#temp + 1] = {X = X1, Y = Y1, Z = Z1}
end
end
end
temp1[Line] = temp
end
temp2[block] = temp1
end
temp3[facedir] = temp2
end
供参考tab = {key = value}
将创建一个表格,将其存储在tab
变量中,密钥为"key"
,变量value
中的值为tab = {[key] = value}
。如果要将变量用作键,则需要使用较长的tab = {["key"] = value}
格式作为键。简单的单词版本只是{{1}}的语法糖。