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

Remote turtle contlroler

Started by grand_mind1, 24 February 2013 - 10:00 AM
grand_mind1 #1
Posted 24 February 2013 - 11:00 AM
I am trying to make a program that allows you to press certain keys while in a computer, and a turtle respond accordingly. I know how to detect the keys and control the turtle with it but I need to know how to loop the detection so that you don't have to start the program every time you want to tell the turtle to do a different action. This is what I have so far:
Computer:

rednet.open("right")
event, code = os.pullEvent("key")
while true do
  if code == 75 then
	rednet.send(30,"Turn Left")
  elseif code == 77 then
	rednet.send(30,"Turn Right")
  elseif code == 76 then
	rednet.send(30,"Go Back")
  elseif code == 72 then
	rednet.send(30,"Go Forward")
  elseif code == 200 then
	rednet.send(30,"Go Up")
  elseif code == 208 then
	rednet.send(30,"Go Down")
  end
end
Turtle:

rednet.open("right")
while true do
  id, msg = rednet.receive()
  if msg == "Go Forward" then
	turtle.forward()
  elseif msg == "Go Back" then
	turtle.back()
  elseif msg == "Turn Left" then
	turtle.turnLeft()
  elseif msg == "Turn Right" then
	turtle.turnRight()
  elseif msg == "Go Down" then
	turtle.down()
  elseif msg == "Go Up" then
	turtle.up()
  elseif msg == "Attack" then
	turtle.attack()
  end
end
I just need to know how to make the computer program not wait for just one key and then stop.
Help is appreciated!
Thanks! :D/>
Doyle3694 #2
Posted 24 February 2013 - 11:18 AM
put
event, code = os.pullEvent("key")
in the first line of your while true do loop, instead of having it above :)/>
What you're basically doing is running a os.pullEvent only once and then looping through checking the same result over and over.
grand_mind1 #3
Posted 24 February 2013 - 11:32 AM
Oops! Sorry, I accidentally posted the wrong code for the computer. Running that code will crash your game because of some weird thing, I don't know exactly why but Cloudy said it was crap code. So ya… That while loop shouldn't be there. Really sorry!
Doyle3694 #4
Posted 24 February 2013 - 11:50 AM
I could imagine, it will spam shitton of rednet messages when you run that code
remiX #5
Posted 24 February 2013 - 11:53 AM
Oops! Sorry, I accidentally posted the wrong code for the computer. Running that code will crash your game because of some weird thing, I don't know exactly why but Cloudy said it was crap code. So ya… That while loop shouldn't be there. Really sorry!

The fix that doyle suggested is the way to fix the program … what are you getting at :X
grand_mind1 #6
Posted 24 February 2013 - 11:58 AM
Oh, ok! I didn't know that was the reason for the crash. Also, what do you mean by "what are you getting at"?
remiX #7
Posted 24 February 2013 - 12:00 PM
Oh, ok! I didn't know that was the reason for the crash. Also, what do you mean by "what are you getting at"?

Crash? What crash :o/> where do you mention a crash - maybe it's from a ton of rednet messages been sent

I mean what was cloudy getting at xD What's wrong with the loop..
grand_mind1 #8
Posted 24 February 2013 - 12:03 PM
I dunno. If you want to read the bug report I posted it's here:
http://www.computercraft.info/forums2/index.php?/topic/10669-15sspcomputers-becoming-inaccessible/
Apparently it's something about taking to much time processing the rednet messages that minecraft can't process anything else. Cloudy said it was my fault and that it was crap code.(I can never tell if that guy is being sarcastic or not… lol)
remiX #9
Posted 24 February 2013 - 12:08 PM
I dunno. If you want to read the bug report I posted it's here:
http://www.computerc...g-inaccessible/
Apparently it's something about taking to much time processing the rednet messages that minecraft can't process anything else. Cloudy said it was my fault and that it was crap code.(I can never tell if that guy is being sarcastic or not… lol)

You're making the server take lots of CPU usage due to crappy code. Good job. Infinite Loop rednet spam!

Lol, yes he said that because the event, code = os.pullEvent() is OUTSIDE the while true do infinite loop.
Because the event pull is outside, it spams which ever message matches up with the code you clicked.

Just move it inside the loop and all shall be fine
grand_mind1 #10
Posted 24 February 2013 - 12:11 PM
Alright, well thanks for your help!