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

[Question] Monitors - .clear() and \n

Started by JTTCOTE, 18 December 2012 - 12:53 PM
JTTCOTE #1
Posted 18 December 2012 - 01:53 PM
I'm trying to design my IC2 room in my house. On the left side I've got:
level monitor monitor lever lever monitor monitor monitor lever
Each of those four levers I've succesfully wired to the rotary macerator, induction furnace et. all on the right wall. I could easily just use signs and check, but I think it would be cooler if I had monitors that said whether the machine was on. Therefore behind the monitors I've got a computer (behind the left one).

For the first monitor pair, the computer has an inverted redstone input from the top and a normal redstone input from its left. This is the code I've put together for that computer(as startup). It's in a loop because I want it to react to redstone changes, if that's not necessary to have it update, please tell me.


monitor.peripheral.wrap("front")
monitor.setTextScale(0.5)
while true do
monitor.write("Left side: Rotary Macerator")
monitor.write("Right side: Induction Furnace")
if redstone.getInput("top")==false then
  monitor.write("Induction Furnace: OFF")
end
if redstone.getInput("top")==true then
  monitor.write("Induction Furnace: ON")
end
if redstone.getInput("left")==true then
  monitor.write("Rotary Macerator: OFF")
end
if redstone.getInput("left")==false then
  monitor.write("Rotary Macerator: ON")
end
sleep(10)
monitor.clear()
end

Now, this runs without errors. However, there are two problems.
1. It prints everything on one long line. I tried adding \n to the end of each line in the if statements, as well as to the beginning of each line, but it still prints on one long line and replaces the \n with a ?.
2. After waiting a bit as it should with sleep(10), it clears the screen and nothing else pops up.

So how do I fix these two?
Edited by
Helfire #2
Posted 18 December 2012 - 02:06 PM
Try:

local curX, curY = monitor.getCursorPos()
monitor.setCursorPos(1, curY+1)

That will move your writes to the next line. You should probably also consider using monitor.getSize() to get the height of the display, and store all lines in a table, so that once you reach the bottom of the monitor you can put in scrolling (Unless you only use a few lines and don't need to scroll ofcourse)

As for your second problem, as you said it all writes on 1 line, so the reason nothing is written after it clears is because it is writing more stuff to the screen, but it's doing it waaaay to the right of the screen (And therefore does not show on the monitor). Doing what I suggested should fix that problem as well.

You could also just make the above code into a function like endLine() so that you simplify your code visually.
theoriginalbit #3
Posted 18 December 2012 - 02:08 PM
ok so problem 1 unreleated to OP:

please try to use code tags when pasting code. code tags are ['code]['/code] to open and close (respectively) without the ' of course.

problem 1 (related)

try using this instead. it should add the \n in

monitor.print()

problem 2:

after clearing the screen reset the cursor to the top again, its probably writing off the screen

term.setCursorPos(1,1)
remiX #4
Posted 18 December 2012 - 02:56 PM
I don't think monitor.print() works for monitors.

I use a simple function to write text to a monitor using arguments, nice and easy :)/>
Try this out:

monitor = peripheral.wrap("front")
monitor.setTextScale(0.5)

function monW(y, text)
    monitor.setCursorPos(1,y)
    monitor.write(text)
end

while true do
    monitor.clear()
    monW(1, "Left side: Rotary Macerator")
    monW(2, "Right side: Induction Furnace")
    if redstone.getInput("top") then
        monW(3, "Induction Furnace: ON")
    else
        monW(3, "Induction Furnace: OFF")
    end
    if redstone.getInput("left") then
        monW(4, "Rotary Macerator: ON")
    else
        monW(4, "Rotary Macerator: OFF")
    end
    sleep(10)
end
theoriginalbit #5
Posted 18 December 2012 - 02:58 PM
I don't think monitor.print() works for monitors.

I could be wrong, but I could have sworn I used it the other day because I found that the \n didn't work. (btw do u reckon thats a bug? if so I think we should report it)
JTTCOTE #6
Posted 18 December 2012 - 02:59 PM
Um, I tried using monitor.print and it gave me attempt to call nil… exact line was


monitor = peripheral.wrap("front")
monitor.setTextScale(0.5)
while true do
monitor.print("Left side: Rotary Macerator")
and then it continues with print replacing write. What's wrong now?


Edit: Two new replies while I typed the above. I'll try remiX's method, but I don't think monitor.print works.
remiX #7
Posted 18 December 2012 - 03:05 PM
Yeah attempt to call nil was the error I got when I tried it quite some time ago. But it's easy to make a print function I guess:


function print(text)
    cX, cY = monitor.getCursorPos()
    monitor.write(text)
    monitor.setCursorPos(1, cY + 1)
end
This will over-ride the print function for printing onto the computers though, so call it something different.
Lyqyd #8
Posted 18 December 2012 - 03:08 PM
I could be wrong, but I could have sworn I used it the other day because I found that the \n didn't work. (btw do u reckon thats a bug? if so I think we should report it)

Not a bug. write() in bios.lua handles newline characters by adjusting the cursor position; term.write shouldn't be fed newline characters.
JTTCOTE #9
Posted 18 December 2012 - 03:13 PM
Yeah, remiX's works fine. Now to adjust it to work with the other one. But yeah. Thanks!
theoriginalbit #10
Posted 18 December 2012 - 03:17 PM
I could be wrong, but I could have sworn I used it the other day because I found that the \n didn't work. (btw do u reckon thats a bug? if so I think we should report it)

Not a bug. write() in bios.lua handles newline characters by adjusting the cursor position; term.write shouldn't be fed newline characters.

ahhh ok.
Kingdaro #11
Posted 18 December 2012 - 04:14 PM
Not a bug. write() in bios.lua handles newline characters by adjusting the cursor position; term.write shouldn't be fed newline characters.
Is there any good reason to not have monitor.print() though?
Lyqyd #12
Posted 18 December 2012 - 05:38 PM
Because when wrapping a monitor object, you get a terminal redirect table, so the .write method is the monitor's equivalent of term.write. You can always redirect to the monitor object, use print, and then restore from it (right away if desired!) to gain that functionality. It's better to have it as a terminal redirect so that you don't have to add every single print() and textutils.tabulate() etc. to the monitor object's table, just the terminal redirect target methods.