0

我正在使用 ComputerCraft,这是一个 Minecraft mod,它添加了计算机、监视器、调制解调器等,可以使用 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("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

帮助将不胜感激。

4

1 回答 1

3

您在 bios.lua 中调用了一个错误,该错误实现了您可以在脚本中使用的功能。喜欢write

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

它说:while string.len(sText) > 0 do,在哪里sText

是第 154 行的输入参数function write( sText )

对于这种情况,没有适当的错误处理或默认值sTextnil. 程序员在这里做的很草率。

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

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

write("something")不会导致任何错误。如果要打印空字符串,只需调用write("").

于 2017-08-26T10:41:40.633 回答