Advertisement
hevohevo

test_msg_sender

Jun 8th, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. -- ###############################
  2. -- msg_sender
  3.  
  4. rednet.open("left")
  5.  
  6. local dest_id = 18
  7. local my_id = os.getComputerID()
  8. local msg1 = "turtle.turnRight()"
  9. local msg2 = "turtle.turnLeft()"
  10. local msg = ""
  11.  
  12. function mysend(dest_id, msg)
  13.   for i=1,5 do
  14.     print(i,":sending to ",dest_id )
  15.     rednet.send(dest_id, msg)
  16.     print(i,': wait')
  17.     local res = {rednet.receive(1)}
  18.     if res and res[2] == msg then return true end
  19.   end
  20.   return false
  21. end
  22.  
  23. while true do
  24.   local event, key_code = os.pullEvent("key")
  25.   local key = keys.getName(key_code)
  26.   if key == "left" then
  27.     msg = msg2
  28.   elseif key == "right" then
  29.     msg = msg1
  30.   else
  31.     error("hoge")
  32.   end
  33.   print("sending...: ", msg)
  34.   if mysend(dest_id, msg) then
  35.     print("succeeded: ", msg)
  36.     func = loadstring(msg)
  37.     func()
  38.   else
  39.     print("failed: ", msg)
  40.   end
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement