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

Issue with monitors in a program

Started by sjjcwatkins, 25 April 2015 - 12:23 PM
sjjcwatkins #1
Posted 25 April 2015 - 02:23 PM
I am working on a computer system that will receive messages, then write them to a screen. When i start the program in a computer all works, except that nothing is printed onto the screen, i have tried a few different things, but it does not work, the code is as follows:

local sid = "6"
local monitor = peripheral.wrap("back")
rednet.open("top")
message = os.pullEvent("")
monitor.setCursorPos(1,1)
monitor.setTextScale(1)
while true do
event, id, text = os.pullEvent()
if id == "sid" then
  if text == "clear" then
   if event == "rednet_message" then
	if text == "clear" then
	 monitor.clear()
	 else
	 monitor.write(message)
	end
   end
  end
end
end
Square789 #2
Posted 25 April 2015 - 08:27 PM
You are checking "sid", which is the sring "sid" on line 9.
Take away the " s and it should work. Maybe it's also better when you remove "" out of the os.pullEvent("")
on line 4.
KingofGamesYami #3
Posted 25 April 2015 - 09:24 PM
os.pullEvent("") is going to yield (aka wait) until it gets the event "". Which obviously won't happen, given that no event does that. (Why are you pulling an event there, anyway?)

Since your script never advances paste line 4, I'd expect it to do nothing.

In addition, id will never equal "sid", assuming you are using the rednet_message event. It will also never equal "6". It might equal 6 (as in the number, not the string) at some point.
HPWebcamAble #4
Posted 25 April 2015 - 10:11 PM
This should do what you are looking for:


local id = 6  --# The id of the computer sending the messages
local m = peripheral.wrap("back") --# Wrap the monitor
rednet.open("top") --# Tell the rednet API where the modem is

m.setTextScale(1)
m.setTextColor(colors.white) --# Color of the monitor text
m.setBackgroundColor(colors.black) --# Color of the monitor background
m.clear() --# Make sure the monitor is cleared to start with
m.setCursorPos(1,1)

while true do
  local event,id,msg = os.pullEvent("rednet_message") --# wait for ONLY rednet messages

  if id == 6 then --# We only care about messages from the id we specify
    if msg == "clear" then
      --# Clear the monitor
      m.clear() 
      m.setCursorPos(1,1)
    else
      --# Print the message
      m.write(message)
      --# Go to the next line
      local x,y = m.getCursorPos()
      m.setCursorPos(1,y+1)
    end
  end
end

Note that it will print the messages it receives, one per line.
If you only want to show the last message, the code would need a little bit of modification