Computercraft Lua将零更改为0
我正在使用Computercraft,一个Minecraft mod,我需要一些帮助。我试图找到一种类型的所有外围设备,从中获取能量,并将它们添加到一起。然而,我得到一个“尝试在nill上执行算术”或一些错误。这是我的密码:Computercraft Lua将零更改为0,lua,computercraft,Lua,Computercraft,我正在使用Computercraft,一个Minecraft mod,我需要一些帮助。我试图找到一种类型的所有外围设备,从中获取能量,并将它们添加到一起。然而,我得到一个“尝试在nill上执行算术”或一些错误。这是我的密码: local periList = peripheral.getNames() energy = 0 totalenergy = 0 for i = 1, #periList do if peripheral.getType(periList[i]) == "cofh_
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)
抱歉,密码箱不工作
不管怎么说,有人知道如何解决这个问题吗?
nil
和“nil”
是两件不同的事情
前者是nil
类型“singleton”,后者是由三个字符组成的字符串。它们并不等同
尝试删除if
行中的引号
此外,您还可以将(电位)nil
指定给energy
,然后直接将energy
设置为0,如果它是nil
(甚至只使用
energy = cell.getEnergyStored("potato") or 0
直接因为
nil
是一个“假y”值,所以nil或0
的计算结果是0
)。nil
和nil
是两个不同的东西
前者是nil
类型“singleton”,后者是由三个字符组成的字符串。它们并不等同
尝试删除if
行中的引号
此外,您还可以将(电位)nil
指定给energy
,然后直接将energy
设置为0,如果它是nil
(甚至只使用
energy = cell.getEnergyStored("potato") or 0
直接因为
nil
是一个“假y”值,所以nil或0
的计算结果为0
)。是的,只需删除引号即可。谢谢我如何才能标记为已解决?@NathanielButterfield在答案旁边的投票箭头下应该有一个复选标记,您可以单击以接受答案。是的,删除引号就行了。谢谢我如何才能将其标记为已解决?@NathanielButterfield在答案旁边的投票箭头下应该有一个复选标记,您可以单击以接受答案。