我正在使用Computercraft,一个Minecraft mod,我需要帮助。 我试图找到一种类型的所有外围设备,从它们获取能量,并将它们加在一起。 但是,我得到了“尝试在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)对不起,codebox无效
无论如何,有谁知道如何解决这个问题?
I am using Computercraft, a Minecraft mod, and I needed help with something. I am attempting to find all peripherals of a type, get the energy from them, and add them together. However, I get a "attempt to perform arithmetic on nill" or something error. Here is my code:
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)Sorry, codebox didn't work
Anyways, does anyone know how to fix this?
最满意答案
nil和"nil"是两个不同的东西。
前者是nil型“singleton”,后者是三个字符串。 它们并不等同。
尝试从if行中删除引号。
你也可以将(潜在的) nil分配给energy然后直接分配energy并将其设置为0(如果它是nil (甚至只是使用)
energy = cell.getEnergyStored("potato") or 0直接因为nil是“false-y”值所以nil or 0计算为0 )。
nil and "nil" are two different things.
The former is the nil type "singleton" the latter is a string of three characters. They are not equivalent.
Try dropping the quotes from the if line.
Also you can assign (the potential) nil to energy and then directly energy and set it to 0 if it is nil (or even just use
energy = cell.getEnergyStored("potato") or 0directly since nil is a "false-y" value so nil or 0 evaluates to 0).
更多推荐
发布评论