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

controlling turtle wirelessly

Started by MinestoPix, 08 December 2012 - 09:09 AM
MinestoPix #1
Posted 08 December 2012 - 10:09 AM
So I want to control a turtle through rednet and I don't know how to make something simple so please help.

for example, I want to control it going forward, digging and turning, can you just do

x,y,z = rednet.receive()
   if y=forward do
   turtle.forward()
   elseif y=dig do
   turtle.dig()
   elseif y=digDown do
   turtle.digDown()
   ....
for every function, or you could do something like

x,y,z = rednet.receive()
   turtle.y()
if you know what I mean?

thank you
OmegaVest #2
Posted 08 December 2012 - 10:20 AM
Well, here's a few tips.

All APIs are stored as tables, so you can use the table calls to make some things happen. For your second example, I'm pretty sure you can use:

x,y,z = rednet.receive(timeout)
turtle[y]

And, if that does not work, you can always create a table that does work that way.


tablet = {
   ["forward"] = {
	  aliases = {"fd", "forward"},  -- How you want to be able to call this action. If there are two of the same alias, the one listed first will get used.
	  func = turtle.forward  --  This is what the action is supposed to be, here, I've made it use turtle.forward() everytime this action is called.
   },
   ["back"] = {
	  aliases = {"bk", "back"},
	  func = turtle.back
   },
   ["180"] = {
      aliases = {"2", "turn 2", "rt 2", "lt 2"},
      func = function(way)   -- Yes, you can make a function inside a table!  Awesome, isn't it?
         if sting.sub(way, 1,2) == "rt" then
            turtle.turnRight()
            turtle.turnRight()
         elseif string.sub(way, 1,2) == "lt" then
            turtle.turnLeft()
            turtle.turnLeft()
         else
            turtle.turnRight()
            turtle.turnRight()
      end
   }
}

Then you can call that, like so.

x, y, z = rednet.receive()
done = false
for k,v in pairs(tablet) do  --  Pulls however many commands you've made.
   for 1, #k.aliases do  -- This will check the aliases for the command sent.
	  if y == k.aliases[i] then 
		 k.func() -- This calls the action itself, and makes the turtle (or computer) do whatever you told it to remember how to do.
         done = true  -- See next line
		 break -- This just closes the loop so multiple actions don't happen.
      end
   end

   if done then  -- Closes outer loop.
      break
   end
end

if not done then  --  If the command was not in the alias list
   print("Command not recognized")
   rednet.send(x, "noact")  -- So the computer you sent from knows its wrong.
end

And, I'll put the explanations up in a moment.

EDIT: Alright, explanations in comments. Reply with further questions, if you have them, or if I have neglected to explain something you don't know.
MinestoPix #3
Posted 08 December 2012 - 10:30 AM
Wow! Thank you so much :)/> .. trying to figure it out :P/> … thanks for effort :D/> I really appreciate it.
MinestoPix #4
Posted 08 December 2012 - 11:00 AM
uhhh .. the last two } are ''unexpected symbols'' for some reason :/ I don't really get it..

oh nvm.. forgot to end the if statement
Edited on 08 December 2012 - 10:06 AM
Lyqyd #5
Posted 08 December 2012 - 11:11 AM
Just use the turtle[y]() thing. Why add all the complexity instead of just indexing the table?
MinestoPix #6
Posted 08 December 2012 - 11:18 AM
oh haha thanks Lyqyd .. I thought it doesn't work because I forgot to write () at the end :)/>