Computercraft Lua将nil改为0(Computercraft Lua change nil to 0)

编程入门 行业动态 更新时间:2024-10-17 16:34:16
Computercraft Lua将nil改为0(Computercraft Lua change nil to 0)

我正在使用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 0

directly since nil is a "false-y" value so nil or 0 evaluates to 0).

更多推荐

本文发布于:2023-07-14 14:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Lua   Computercraft   change   nil

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!