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

Printer is not printing when it should

Started by Wilobate, 16 April 2017 - 04:51 AM
Wilobate #1
Posted 16 April 2017 - 06:51 AM
So, i have a password door lock, i have clients and server. Im trying to make the server print a log.
I have tried many many many different things, and i just cant seem to work out why. If you have an easier way of doing it, please share :)/>

It is printing
Log: 0
————————-



And blank for the rest of the page.

Code is as follows


term.clear()
term.setCursorPos(1,1)
printer = peripheral.wrap("right")

ready = true
log = 0
lines = 0
active = false

function newLine (line)
  local x, y = printer.getCursorPos()
  printer.setCursorPos(x,lines+1)
  printer.write(line)
  lines = lines + 1
  if lines == 21 then
	printPage()
  end
end

function newPrint ()
  printer.newPage()
  printer.setCursorPos(1,1)
  printer.setPageTitle("Password Server 1")
  printer.write("Log: " ..log)
  printer.setCursorPos(1,2)
  printer.write("-------------------------")
  lines = lines + 2
  ready = false
  active = true
end

function printPage ()
  printer.endPage()
  log = log + 1
  lines = 0
  active = false
  ready = true
end

print("Password Server 1")

rednet.open("top")
while true do
  local sender, pass = rednet.receive()
  if ready then
	newPrint()
  end
  if pass == "w1ll" then
	print("[" ..sender.. "] Got code: 0.")
	rednet.send(sender,"0")
	if active then
	  write = "["..sender.."] Got code: 0."
	  newLine(write)
	end
  else
	print("[" ..sender.. "] Got code: 1.")
	rednet.send(sender,"1")
	if active then
	  write = "["..sender.."] Got code: 1."
	  newLine(write)
	end
  end
end
Edited on 16 April 2017 - 04:54 AM
Bomb Bloke #2
Posted 16 April 2017 - 07:41 AM
In newLine(), change:

  local x, y = printer.getCursorPos()
  printer.setCursorPos(x,lines+1)

… to:

  printer.setCursorPos(1,lines+1)

It'd also pay not to overwrite "write". By default, that variable holds a pointer leading to a rather important function.
Wilobate #3
Posted 16 April 2017 - 07:53 AM
In newLine(), change:

  local x, y = printer.getCursorPos()
  printer.setCursorPos(x,lines+1)

… to:

  printer.setCursorPos(1,lines+1)

It'd also pay not to overwrite "write". By default, that variable holds a pointer leading to a rather important function.

So dont use write as a parameter?

ill give i a shot

EDIT: Works perfect now. Still confused as to why this code didnt work
  local x, y = printer.getCursorPos()
  printer.setCursorPos(x,lines+1)
Edited on 16 April 2017 - 05:58 AM
Bomb Bloke #4
Posted 16 April 2017 - 09:52 AM
Still confused as to why this code didnt work

Because it moves the cursor down a row while keeping it in the same column. Since you never move it back to the left-hand margin, that means that once you've printed a sufficient amount of text (eg your great long row of dashes), the cursor moves off the right-hand edge of the page and anything written after that will not be seen.

That is to say, like term.write(), printer.write() doesn't implement word-wrap for you.
Wilobate #5
Posted 16 April 2017 - 09:59 AM
Still confused as to why this code didnt work

Because it moves the cursor down a row while keeping it in the same column. Since you never move it back to the left-hand margin, that means that once you've printed a sufficient amount of text (eg your great long row of dashes), the cursor moves off the right-hand edge of the page and anything written after that will not be seen.

That is to say, like term.write(), printer.write() doesn't implement word-wrap for you.

Aaaaaahhhhh, right. Makes sense, i thought that it resets itself back to the left side. Thank you