positions = {
--table 1
[1] = {pos = {fromPosition = {x=1809, y=317, z=8},toPosition = {x=1818, y=331, z=8}}, m = {"100 monster"}},
--table 2
[2] = {pos = {fromPosition = {x=1809, y=317, z=8},toPosition = {x=1818, y=331, z=8}}, m = {"100 monster"}},
-- table3
[3] = {pos = {fromPosition = {x=1809, y=317, z=8},toPosition = {x=1818, y=331, z=8}}, m = {"100 monster"}}
}
tb = positions[?]--what need place here?
for _,x in pairs(tb.m) do --function
for s = 1, tonumber(x:match("%d+")) do
pos = {x = math.random(tb.pos.fromPosition.x, tb.pos.toPosition.x), y = math.random(tb.pos.fromPosition.y, tb1.pos.toPosition.y), z = tb.pos.fromPosition.z}
doCreateMonster(x:match("%s(.+)"), pos)
end
end
这里的问题是,我使用tb = positions [1],它只适用于"位置"表。但是如何将此函数应用于此表中的所有表?
答案 0 :(得分:2)
我不太了解Lua,但你可以在桌子上循环:
for i = 0, table.getn(positions), 1 do
tb = positions[i]
...
end
资料来源: http://lua.gts-stolberg.de/en/schleifen.php和http://www.lua.org/pil/19.1.html
答案 1 :(得分:2)
您需要使用数字positions
迭代for
。
请注意,与Antoine Lassauzay的答案不同,循环从 1 而非 0 开始,并使用#
运算符代替table.getn
(Lua 5.1中已弃用的功能,在Lua 5.2中删除)。
for i=1,#positions do
tb = positions[i]
...
end
答案 2 :(得分:0)
使用内置的pairs()。没有任何理由在这里做一个数字for循环。
for index, position in pairs(positions) do
tb = positions[index]
-- tb is now exactly the same value as variable 'position'
end