LUA基础问题

我正在使用ComputerCraft这个Minecraft模组,它添加了计算机、监视器、调制解调器等等设备,可以使用Lua脚本进行编程。

http://www.computercraft.info/wiki/Main_Page

在运行我的脚本时,我遇到了这个错误:“bios:171: bad argument: string expected, got nil”。

我不明白,因为它说在第171行,即使我的代码没有超过30行…有人能解释一下吗?

monitor = peripheral.wrap("right")
monitor.clear()
monitor.setCursorPos(1, 1)
monitor.setTextScale(1)
monitor.write("当前模式:")
monitor.setCursorPos(1, 3)
monitor.write("熔炉")
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("你失败了")
end

非常感谢帮助。

点赞
用户2858170
用户2858170

你在 bios.lua 中调用了一个错误,该文件实现了在你的脚本中可以使用的函数,如 write

如果我们查看 bios.lua,我们会发现第 171 行实际上是 write 实现的一部分。

它说:“while string.len(sText) > 0 do”,其中 sText 是输入参数,位于第 154 行的 function write( sText ) 中。

sText 为空的情况下,没有适当的错误处理或默认值。程序员在这里做得很草率。

在这种情况下,第 171 行中的 string.len(sText) 将导致您收到的错误。

为了解决这个问题,你要么删除对 write 的空调用,这相当于 write(nil),要么提供一些输入字符串。

write("something") 不会引起任何错误。如果你想打印一个空字符串,只需调用 write("")

2017-08-26 10:41:40