我如何在程序上迭代Lua中的类似变量?

时间:2014-09-01 08:57:16

标签: lua computercraft

我有很多变量似乎更容易被引用,但我不知道该怎么做;变量名与坐标系上的条目相关,它们写入的位置也是基于坐标的。我试过搜索谷歌,我已经浏览了Lua文档,但由于我不确定我在寻找什么,我认为这会妨碍我的搜索。以下是变量在我的代码摘录中的显示方式:

- 银行1

local Object111 =  sItem.getTargetDetails("-4,3,-4")
local Object112 =  sItem.getTargetDetails("-5,3,-4")
local Object113 =  sItem.getTargetDetails("-6,3,-4")
local Object114 =  sItem.getTargetDetails("-7,3,-4")
local Object115 =  sItem.getTargetDetails("-8,3,-4")

local Object121 =  sItem.getTargetDetails("-4,4,-4")
local Object122 =  sItem.getTargetDetails("-5,4,-4")
local Object123 =  sItem.getTargetDetails("-6,4,-4")
local Object124 =  sItem.getTargetDetails("-7,4,-4")
local Object125 =  sItem.getTargetDetails("-8,4,-4")

local Object131 =  sItem.getTargetDetails("-4,5,-4")
local Object132 =  sItem.getTargetDetails("-5,5,-4")
local Object133 =  sItem.getTargetDetails("-6,5,-4")
local Object134 =  sItem.getTargetDetails("-7,5,-4")
local Object135 =  sItem.getTargetDetails("-8,5,-4")

- 银行2

local Object211 =  sItem.getTargetDetails("-4,3,1")
local Object212 =  sItem.getTargetDetails("-5,3,1")
local Object213 =  sItem.getTargetDetails("-6,3,1")
local Object214 =  sItem.getTargetDetails("-7,3,1")
local Object215 =  sItem.getTargetDetails("-8,3,1")

local Object221 =  sItem.getTargetDetails("-4,4,1")
local Object222 =  sItem.getTargetDetails("-5,4,1")
local Object223 =  sItem.getTargetDetails("-6,4,1")
local Object224 =  sItem.getTargetDetails("-7,4,1")
local Object225 =  sItem.getTargetDetails("-8,4,1")

local Object231 =  sItem.getTargetDetails("-4,5,1")
local Object232 =  sItem.getTargetDetails("-5,5,1")
local Object233 =  sItem.getTargetDetails("-6,5,1")
local Object234 =  sItem.getTargetDetails("-7,5,1")
local Object235 =  sItem.getTargetDetails("-8,5,1")

然后我会继续在一个函数中调用这些变量,其中屏幕上的位置是名称中数字的函数,前两个数字与x coords相关,最后一个数字与y coords: / p>

mon.setCursorPos(4,4)
mon.write(Object111.StoredPercentage)
mon.setCursorPos(10,4)
mon.write(Object112.StoredPercentage)
mon.setCursorPos(16,4)
mon.write(Object113.StoredPercentage)
...
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
...
mon.setCursorPos(36,4)
mon.write(Object211.StoredPercentage)
mon.setCursorPos(42,4)
mon.write(Object212.StoredPercentage)
mon.setCursorPos(48,4)
mon.write(Object213.StoredPercentage)
etc....

我可以看到这应该可以动态生成,但我不知道从哪里开始而不是手动调出;如果我的代码更清晰,我会更喜欢它。在这个阶段,我真的需要被教导如何钓鱼;如果有人能指出我解释如何做我想做的事情的文件,我将非常感激。

1 个答案:

答案 0 :(得分:3)

对于任何想要实现相同目标的人来说,这是我的最终解决方案:

local Banks = 2
local Rows = 3
local Columns = 5

function Objectstatus(bank,row,column)

    Id = bank .. row .. column
    worldx = "-" .. column+3
    worldy = row+2
    if bank == 1 then worldz = -4; end
    if bank == 2 then worldz = 1; end
    Object = {}
    Object[Id] = s.getTargetDetails(worldx..","..worldy..","..worldz)
    xcursor = (column*7)+3
    if bank == 1 then 
        ycursor = (row*4)+8
    end
    if bank == 2 then 
        ycursor = (row*4)+24
    end

    mon.setCursorPos(xcursor,ycursor)
    powertext(Object[Id].StoredPercentage) -- A function that sets Texts settings based on result
    mon.write(Object[Id].StoredPercentage) -- Actually writes the result
end

    for BankTemp = 1, Banks do
        for ColumnTemp = 1, Columns do
            for RowTemp = 1, Rows do
            Objectstatus(BankTemp,RowTemp,ColumnTemp)
            end
        end
    end