基本的LUA问题 [英] Basic LUA problems
问题描述
我正在使用ComputerCraft,这是Minecraft的Mod,它添加了计算机,显示器,调制解调器等,可以使用Lua脚本进行编程.
I'm using ComputerCraft, a Minecraft mod that adds computers, monitors, modems and so on, which can be programmed using Lua scripts.
http://www.computercraft.info/wiki/Main_Page
在运行脚本时,出现以下错误:"bios:171:错误参数:预期为字符串,为nil".
While running my script I get this error: "bios:171: bad argument: string expected, got nil".
我不明白,因为即使我的代码不超过30行,它也会显示第171行.有人可以解释吗?
I dont understand because it says line 171 even though my code doesn't exceed 30 lines.. Can somebody explain?
monitor = peripheral.wrap("right")
monitor.clear()
monitor.setCursorPos(1, 1)
monitor.setTextScale(1)
monitor.write("Current mode:")
monitor.setCursorPos(1, 3)
monitor.write("furnace")
redstone.setOutput("back", false)
print("blablabla")
write()
if input == ja then
print("k")
redstone.setOutput("back", true)
monitor.clear()
monitor.setCursorPos(1, 1)
monitor.write("blabla")
else
print("u sux")
end
我们将不胜感激.
推荐答案
您在bios.lua中调用了一个错误,该错误实现了可以在脚本中使用的功能.就像write
.
You invoked an error in bios.lua which implements functions you can use in your script. Like write
.
如果我们查看 bios.lua 我们看到171行实际上是write
实现的一部分.
If we take a look into bios.lua we see that line 171 is actually part of the implementation of write
.
它说:while string.len(sText) > 0 do
,其中sText
是第154行中function write( sText )
的输入参数.
is the input argument to function write( sText )
in line 154.
对于sText
为nil
的情况,没有适当的错误处理或默认值.程序员在这里做得很草率.
There is no proper error handling or default value for the case that sText
is nil
. The programmer did a sloppy job here.
在这种情况下,第171行中的string.len(sText)
会导致您收到错误.
In that case string.len(sText)
in line 171 will causes the error you received.
为了解决此问题,您必须删除对write
的空调用(等效于write(nil)
),或者必须提供一些输入字符串.
In order to fix that you have to either remove the empty call to write
, which is equivalent to write(nil)
or you have to provide some input string.
write("something")
不会引起任何错误.如果要打印一个空字符串,只需调用write("")
.
write("something")
will not cause any error. If you want to print an empty string just call write("")
.
这篇关于基本的LUA问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!