Advertisement
Helium

Rednet listener

May 28th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. -- Listener for rednet
  2.  
  3. -- initiating variables
  4. messages = {}
  5. line = 1
  6.  
  7. -- monitor and rednet setup
  8. rednet.open("back")
  9.  
  10. monitor = peripheral.wrap("top")
  11. monitor.clear()
  12. monitor.setTextScale(1)
  13. monitor.setCursorPos(1, 1)
  14.  
  15. -- defining functions
  16. function displayMsg(screen, msg, id)
  17.  
  18.     print("displaying messages")
  19.    
  20.     for i = 1, 12 do
  21.         monitor.setCursorPos(1, i)
  22.         monitor.setTextColor(id)
  23.         monitor.write(msg[i])
  24.        
  25.         print("message displayed: ".. msg[i])
  26.     end
  27. end
  28.  
  29. function scroll(msg)
  30.    
  31.     print("scrolled")
  32.    
  33.     for i = 1, 12 do
  34.         msg[i] = msg[i+1]
  35.     end
  36.     return msg
  37. end
  38.  
  39. -- main program loop
  40. while true do
  41.  
  42.     event, id, message = os.pullEvent()
  43.    
  44.     if event == "rednet_message" then
  45.    
  46.         print("message received")
  47.    
  48.         newMsg = tostring(id.." > "..message)
  49.         messages[line] = newMsg
  50.        
  51.         print("message stored")
  52.        
  53.         if line < 12 then
  54.             line = line + 1
  55.             print("adding additional line")
  56.         elseif line == 12 then
  57.             messages = scroll(messages)
  58.         else
  59.             print("line scrolling error")
  60.         end
  61.  
  62.         displayMsg(monitor, messages, id)
  63.        
  64.         print("text displayed")
  65.     end
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement