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

Term.redirect failing...

Started by cmckain14, 02 December 2014 - 11:22 PM
cmckain14 #1
Posted 03 December 2014 - 12:22 AM
I'm attempting to have my program print normally, then print into a window on the right of the screen, then switch back and print normally again. Every time, the program just prints the third line of text with the config of the window even with 'term.redirect(origin)'.


--Functions--
function names()
			    names = { "Bob", "Jim", "Jeb" }
			    end
function wwrite(text,color) --This function!!!
			    local origin = term.current()
			    term.redirect(myWindow)
			    if color ~= nil then
							    myWindow.setTextColor(color)
							    end
			    local posx,posy = myWindow.getCursorPos()
			    posy = posy + 1
			    myWindow.setCursorPos(1,posy)
			    myWindow.write(text)
			    term.redirect(origin)
			    --term.redirect(term.native())
			    term.setCursorPos(x,y)
end
function host(protocol)
			    term.redirect(term.native())
			    if not rednet.isOpen(side) then
							    rednet.open(side)
							    end
			    rednet.host(protocol,name)
			    term.write("Hosting the ")
			    if protocol == "Check-in" then
							    term.setTextColor(colors.green)
			    elseif protocol == "Alert" then
							    term.setTextColor(colors.red)
			    else
							    term.setTextColor(colors.orange)
							    end
			    term.write(protocol)
			    term.setTextColor(colors.white)
			    term.write(" channel.")
			    x,y = term.getCursorPos()
			    x = x - x + 1
			    y = y + 1
			    term.setCursorPos(x,y)
end
function listen(what)
			    if what == nil then
							    rednet.receive()
			    --elseif what
							    end
end
function checkin()
			    names()
			    i = 0
			    repeat
							    i = i + 2
							    id,message = rednet.receive("Check-in")
							    if message == true then
											    guard[i] = id
											    local y = id + 1
											    wwrite(id.."-"..names[id])
			    							    print("Guard "..id.." checked in.")
											    message = tostring(message)
											    end
							    --print(id.." "..message)
			    until nog == i
end
function startup()
			    guard = {}
			    host("Check-in")
			    local x,y = term.getSize()
			    restoreTo = term.current()
			    myWindow = window.create(term.current(),32,1,25,10,true)
			    myWindow.setBackgroundColor(colors.gray)
			    myWindow.clear()
			    wwrite("Guards:")
			    host("Alert")
			    checkin()
end
--Settings--
name = "GuardServer"
nog = 1 --Number of guards
side = "bottom"
--monitorside = "top"

--monitor = peripheral.wrap("top")
startup()
Thanks for any help.
Edited on 02 December 2014 - 11:23 PM
Bomb Bloke #2
Posted 03 December 2014 - 01:45 AM
This sort of code strikes me as odd:

x = x - x + 1  -- x will always be set to 1...

At the bottom of wwrite(), we have:

term.setCursorPos(x,y)

x and y aren't referenced anywhere else in wwrite, so these will be referring to the global versions - which you happened to define in the host() function. I'm not entirely sure why you'd want to keep putting the cursor back there.

It may or may not be worth noting that when you ask a window to do something, it does it by asking its parent to do it. Say you have a window located at (10,12), and you set the cursor in that window to (5,8) - it'll do it by asking its parent to move its cursor to (14,19).
cmckain14 #3
Posted 03 December 2014 - 04:06 AM
So what would be causing the second statement to have the background color of the window? (Note the photo)
This sort of code strikes me as odd:

x = x - x +1 x will always be set to 1...
This code should be y instead of x, to move it down one line.

It may or may not be worth noting that when you ask a window to do something, it does it by asking its parent to do it. Say you have a window located at (10,12), and you set the cursor in that window to (5,8) - it'll do it by asking its parent to move its cursor to (14,19).
So what would be the solution?

Thanks
Edited on 03 December 2014 - 03:12 AM
Bomb Bloke #4
Posted 03 December 2014 - 05:56 AM
So what would be causing the second statement to have the background color of the window? (Note the photo)

When you say "the second statement", do you mean the line reading "hosting the alert channel"? If so, it gets a grey background for the reasons I explained: You set the background colour of the window to grey. The window operates by having its parent - in this case the original terminal - do things for it. Therefore, setting the window's background colour to grey also sets the default terminal's background colour to grey.

This code should be y instead of x, to move it down one line.

Erm, that's not what I'm getting at. If you take any number, minus itself and then add one, you'll always end up with one. So why not skip the math and just say "one"?

So what would be the solution?

Well, one way to easily manage things is to use multiple windows, both with the default terminal as their parents. You then never write directly to that "parent" terminal - instead you use the windows for everything, as they won't affect each other like they do their parent.