Function 对于Lua中函数内部的循环,Computercraft

Function 对于Lua中函数内部的循环,Computercraft,function,lua,computercraft,Function,Lua,Computercraft,我正在学习计算机编程(minecraft),在读取一些存储单元时遇到了一些问题 我正在使用的函数将遍历所有单元格,并将存储容量添加到for循环中的一个变量中 这就是我目前得到的 local cell1 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_2") local cell2 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_3") local

我正在学习计算机编程(minecraft),在读取一些存储单元时遇到了一些问题

我正在使用的函数将遍历所有单元格,并将存储容量添加到for循环中的一个变量中

这就是我目前得到的

local cell1 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_2")
local cell2 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_3")
local cell3 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_4")
local cell4 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_5")
local cell5 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_6")
local cell6 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_7")

cells = {"cell1", "cell2", "cell3", "cell4", "cell5", "cell6"}

local totStorage

function getTotStorage(table)
    for key = 1,6 do
        x = table[key]
        totStorage = totStorage + x.getMaxEnergyStored()        
    end
    print(totStorage)
end
我在这条线上遇到一个错误

totStorage = totStorage + x.getMaxEnergyStored()    
说“尝试呼叫零”。 有什么建议吗

cells = {"cell1", "cell2", "cell3", "cell4", "cell5", "cell6"}
是以下的简写:

cells = {
-- key   value
   [1] = "cell1", 
   [2] = "cell2", 
   [3] = "cell3", 
   [4] = "cell4", 
   [5] = "cell5", 
   [6] = "cell6"
}
假设您的
getTotStorage
调用与下面类似(您没有发布它)

在循环中,您尝试在
x
上调用一个方法,该方法是一个字符串值:

for key = 1,6 do
   x = table[key]
   totStorage = totStorage + x.getMaxEnergyStored()        
end
这基本上是在努力做到:

totStorage = totStorage + ("cell1").getMaxEnergyStored()
您可以做的是重新排列代码,使值成为
外围设备返回的对象。wrap

local cells = {
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_2"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_3"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_4"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_5"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_6"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_7"),
}

function getTotStorage(t)
  local totStorage = 0
  for i,v in ipairs(t) do
     totStorage = totStorage + v.getMaxEnergyStored()
  end
  print(totStorage)
end
一些意见:

  • 可以(即
    totStorage
    )限制变量范围时,使用
    local
    (无需将循环计数器作为全局计数器)
  • 不要命名与标准Lua库冲突的变量(即
    字符串
    表格
    数学
  • ipairs
    是在序列中循环的更好方法
local cells = {
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_2"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_3"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_4"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_5"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_6"),
  peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_7"),
}

function getTotStorage(t)
  local totStorage = 0
  for i,v in ipairs(t) do
     totStorage = totStorage + v.getMaxEnergyStored()
  end
  print(totStorage)
end