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

[1.42] printer tutorial

Started by GopherAtl, 23 September 2012 - 06:33 PM
GopherAtl #1
Posted 23 September 2012 - 08:33 PM
Surprised this hasn't been done already, so I'll have a go. It's pretty simple really.

First, like most peripherals, you have to wrap it first. If it's on the right side, you'd do…

local printer=peripheral.wrap("right")

If you just want to check for one on any side and wrap it, you could do this


--declare variable out here, let it default to nil
local printer

--for each side - redstone.sides() returns a table listing all of them
for _,side in pairs(redstone.getSides()) do
  --check if it's a printer...
  if peripheral.getType(side)=="printer" then
	--it is! wrap it and break out of the loop
	printer=peripheral.wrap(side)
	break
  end
end

--check if you got one
if printer==nil then
  --nope! exit out of the program
  error("No printer detected!")
end

Now that you've got the printer wrapped, you have to load ink and paper into it. Right-click the printer to pull up the gui, put some paper in the slots at the top and ink in the slot on the left. You can put any color ink in the printer, but only one color at a time.

In lua, you can get the amount of ink and paper in the printer with the getInkLevel and getPaperLevel functions


--repeat while there is no ink
if printer.getInkLevel()==0 then
  print("No ink! Add some ink to the left slot to continue...")
  while printer.getInkLevel()==0 do
	os.sleep(.5)
  end
end

--do same for paper
if printer.getPaperLevel()==0 then
  print("No paper! Add some paper to the top slots to continue...")
  while printer.getPaperLevel()==0 do
	os.sleep(.5)
  end
end

Now you're ready to print. First, you will always have to call newPage. This will cause the printer to "consume" one piece of paper and one ink. Once you've called newPrint, you're locked in to printing with the color dye that was loaded when you called it. Changing dyes after newPage will do nothing.


--start the page
printer.newPage()

Once you've called newPage, you can call printer.write() to write text to the printer, and printer.setCursorPos() to move the print cursor around the page. You can also use printer.getCursorPos() and printer.getPageSize() to find out where the cursor is and the dimensions of the page. Note that print.write works just like term.write() or monitor.write() - it does not wrap lines, and if your text goes off the right edge, it will just disappear!


printer.write("Hello, world!")
printer.setCursorPos(1,2)
printer.write("I'm a printer.")
local w, h=printer.getPageSize()
printer.setCursorPos(1,h)
printer.write("This is the bottom line")

local str="Vertical"
for i=1,#str do
  printer.setCursorPos(w,i)
  printer.write(string.sub(str,i,i))
end

You can also set the title of the page - this won't appear on the page itself, but works like computer and disk labels, appearing as the name of the printed page when you hover on it in your inventory


printer.setPageTitle("Tutorial Printout")

When you've written all you want on the page, end it by calling endPage


printer.endPage()

The bottom row of the printer GUI will now contain a page labeled "Tutorial Printout." Grab it to your inventory and right-click while holding it to read, just like a book. And that's the basics of printing!

About color. As I showed you above, you can only print one color at a time. However, you can load any color dye to print with. You can also put printed pages from output back into the input page and print on them again. This means it is possible to print multi-color documents, but you have to print the page over again, once for each color! It doesn't remember the cursor position or anything, so it's up to your program to call setCursorPos() and avoid writing over the previous text on each following print. I may write a more advanced tutorial on how to do that later, but stopping here for now.

Here's all the code blocks above cobbled together into an example program you can download, study, and modify.

http://pastebin.com/fX3p7nYC
Jan #2
Posted 23 September 2012 - 09:02 PM
That's a nice tutorial you wrote, easy to understand and not too long.
adus #3
Posted 15 November 2012 - 06:14 PM
you made a typo:
says:
if paper.getPaperLevel()==0 then

Should be:
if printer.getPaperLevel()==0 then
GopherAtl #4
Posted 15 November 2012 - 07:39 PM
thanks; fixed
XeriosValentine #5
Posted 04 December 2012 - 03:59 AM
Very easy to understand I'll study ur program code more & will made a more variabel form of this for my little project~
Thx for the tut^^
xxxredportalxxx #6
Posted 16 January 2013 - 07:39 PM
Very usefull and easy to follow thx Gopher :)/>
radiation975 #7
Posted 02 May 2013 - 04:31 PM
is it possible to make a similar program that can print anything you tell it to print? I tried to make such a program and it wouldn't work no matter what i did. I even fixed my code based on the error logs and it still wouldn't work as intended.