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

Is there a way to wirelessly send a line of lua as a string to a turtle and have it execute it?

Started by farigiss, 02 January 2013 - 06:37 AM
farigiss #1
Posted 02 January 2013 - 07:37 AM
[Solved]

I have a wireless computer that I want to control all my turtles with. I've written a program on that computer that can command all listening turtles to run a program.

Here's a simplified version of what I already have.

Computer: (ID: 1)

rednet.send(2, "dance")


Turtle: (ID 2)

while true do
  command = rednet.receive()
  shell.run(command)
end

—-

The only problem is that the above only works for programs that are already on the turtle.

That's why I want to be able to send any line of lua to the turtle, have the turtle interpret it and run it, like so:

Computer: (ID: 1)

command = "turtle.dig()"  -- I want to send this string to the turtle
rednet.send(2, command)

Turtle: (ID: 2)


while true do
  command = rednet.receive()
  interpretAndRunLuaCode(command) -- I want the turtle to execute the string
end

I realise the fs API can be used to create a file, write the line of lua to it, and then execute it as a program and that should work. I just want to know if there's a better way.

Thanks.
Doyle3694 #2
Posted 02 January 2013 - 07:56 AM
loadstring(command)()

Though in your current setup, command would be the ID of the sender.
remiX #3
Posted 02 January 2013 - 08:18 AM
Yes, rednet.receive() returns 3 values, senderID, message, and the distance


while true do
  senderID, command = rednet.receive()
  shell.run(command)
end
farigiss #4
Posted 02 January 2013 - 08:27 AM
loadstring(command)()

Though in your current setup, command would be the ID of the sender.

Loadstring works! Thanks
Yes, rednet.receive() returns 3 values, senderID, message, and the distance


while true do
  senderID, command = rednet.receive()
  shell.run(command)
end
Woops, mistake. I actually did it correctly in the actual program.