This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CCJJSax's profile picture

Read() As Lua

Started by CCJJSax, 31 August 2013 - 03:18 PM
CCJJSax #1
Posted 31 August 2013 - 05:18 PM
I would like to have some way that you could use a read() or similar input method and have a turtle use it like it was in the lua interpreter. I'm not using read() I'm actually using chat(message) from misc peripherals. I would like to be able to give this turtle commands like I was in lua.
Kingdaro #2
Posted 31 August 2013 - 05:24 PM
You can run lua commands using loadstring(). loadstring() returns a function, which you can call to then run the specified string.


local cmd = "print('Hello world!')"
local func = loadstring(cmd)
func() --> Hello world!

If you want to run a string without crashing, use pcall().


local ok, err = pcall(func)
if not ok then
  print(err)
end

pcall() returns true or false, whether the function ran successfully or not. If ok is false, err is the error message.