2014-09-01 21 views
2

我有很多變量可以讓自己更容易被引用,但我不知道如何去做;變量名稱與座標系上的條目相關,並且它們寫入的位置也是基於座標的。我試圖搜索谷歌,我已經通過了Lua文檔,但因爲我不確定我在找什麼,我認爲這阻礙了我的搜索。下面是這些變量如何看在我的代碼的摘錄:如何在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座標有關的前兩個數字和y座標的最後一個數字:

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.... 

我可以看到,這應該可以在飛行中生成,但我不知道從哪裏開始,而無需手動調用;如果我的代碼更乾淨,我更喜歡它。在這個階段,我真的只需要教會如何釣魚;如果任何人都可以指出我解釋如何做我想做的事的文件,我會非常感激。

+3

有訪問''在當地Lua'的variables'的方式。檢查[PiL:debug.getlocal()](http://www.lua.org/pil/23.1.1.html)。另一個類似的已經回答的問題是[here](http://stackoverflow.com/a/2835433/1150918)。如果我是你,我會盡量避免這種凝結,並將這些對象放到本地範圍表中。 – Kamiccolo 2014-09-01 09:11:54

+1

是的!我真的顯示出我的希望......桌子是解決我的問題的第一步;我有一些閱讀要做。你能給我一些關於如何分解一個關聯數組變量名的指針,這樣我可以從Object111中取出最後一個數來計算x座標,前兩個計算y座標?謝謝! – fileinster 2014-09-01 09:41:42

+0

別擔心......我明白了!一旦完成,我會發布我的結果。再次感謝...你給了我所有我需要的小動作。 – fileinster 2014-09-01 10:27:24

回答

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 
+0

一個小小的建議 - 我會做'Id','worldx','worldy','xcursor','ycursor''本地''也是。這樣就不會堵塞「全局」空間並且執行速度更快一點。 – Kamiccolo 2014-09-02 09:12:46

+0

我編寫腳本的方式意味着這些變量在函數之外被調用,我相信這意味着除非我誤認爲它們必須是全局的。但是,感謝持續的建議,因爲它受到了熱烈的歡迎。 – fileinster 2014-09-02 10:10:03

+1

uffff ...這不是最佳做法。你知道,在Lua函數中可以返回多個值嗎?參考文獻:PiL5.1 - [多個結果](http://www.lua.org/pil/5.1.html) – Kamiccolo 2014-09-02 13:27:10