1

我正在使用 Minecraft 模组 Computercraft,我需要一些帮助。我试图找到一种类型的所有外围设备,从中获取能量,然后将它们加在一起。但是,我收到“尝试对 nill 执行算术”或其他错误。这是我的代码:

local periList = peripheral.getNames()
energy = 0
totalenergy = 0

for i = 1, #periList do
  if peripheral.getType(periList[i]) == "cofh_thermalexpansion_energycell" then
    local cell = peripheral.wrap(periList[i])
    print(periList[i])
    if cell.getEnergyStored("potato") ~= "nil" then
      energy = cell.getEnergyStored("potato")
      print(cell.getEnergyStored("potato"))
    else
      energy = 0
      print(0)
    end
    totalenergy = totalenergy + energy
  end
end
print(totalenergy)

抱歉,密码箱不起作用

无论如何,有人知道如何解决这个问题吗?

4

1 回答 1

4

nil"nil"是两个不同的东西。

前者是nil“singleton”类型,后者是三个字符的字符串。它们不是等价的。

尝试从行中删除引号if

您也可以将(潜力)分配给nil然后energy直接energy将其设置为 0(如果是)nil(或者甚至只是使用

energy = cell.getEnergyStored("potato") or 0

直接因为nil是“false-y”值,所以nil or 0计算结果为0)。

于 2014-09-04T23:39:50.287 回答