Basic LUA problems

流过昼夜 提交于 2019-12-11 16:54:43

问题


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

While running my script I get this error: "bios:171: bad argument: string expected, got nil".

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

help would be highly appreciated.


回答1:


You invoked an error in bios.lua which implements functions you can use in your script. Like write.

If we take a look into bios.lua we see that line 171 is actually part of the implementation of write.

It says: while string.len(sText) > 0 do, where sText

is the input argument to function write( sText ) in line 154.

There is no proper error handling or default value for the case that sText is nil. The programmer did a sloppy job here.

In that case string.len(sText) in line 171 will causes the error you received.

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") will not cause any error. If you want to print an empty string just call write("").



来源:https://stackoverflow.com/questions/45894188/basic-lua-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!