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

[Turtle][Programming] Command a turtle

Started by TheEisbaer, 20 October 2012 - 03:12 PM
TheEisbaer #1
Posted 20 October 2012 - 05:12 PM
Hey pros out there,

I have a question: How do i make a turtle (wireless turtle) do something when a Computer with a modem send something to the turtle.

Turtle ID 34
Computer ID 33

Thanks

TheEisbaer
cheekycharlie101 #2
Posted 20 October 2012 - 05:34 PM
I am coding a turtle control system at the moment. as soon as its finished il pm it to you :)/>/>
ChunLing #3
Posted 20 October 2012 - 06:19 PM
There are a number of remote control programs for turtles that use different methods. I recommend:
while rcID do sndr,rnmssg = rednet.receive()
		if sndr == rcID then
			_,_,cmnd, prm1 = rnmssg:find("^(%a+)(.*)")
			if turtle[cmnd] then turtle[cmnd](tonumber(prm1))
	   	 elseif rnmssg == "trmn8RC" then rcID = nil end
		end
end
rcID is the ID of the controller for the turtle, sndr is the ID of a rednet message, rnmssg is the message. If the message is from the correct ID, then parse the message into a command (cmnd) and parameter (prm1). If the command is a turtle API function, then execute that command with the parameter (after converting it to a number). Of note, you only need to send the part of the function name that comes after 'turtle.'. So to do turtle.forward(), just send "forward". The parameter isn't used for most functions, but when it should be a number. You can separate the number from the command with a space (or not). So "select 8" will call turtle.select(8) and then "drop 10" will call turtle.drop(10).

If the message didn't parse as a turtle function, then check if it was the terminate command to end remote control.