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

How to make a buffer

Started by Jummit, 05 August 2017 - 04:43 PM
Jummit #1
Posted 05 August 2017 - 06:43 PM
I use buffers in some programms, but i don't know if this is the best method. Here is the code:

local buffer = window.create(term.current(), 1, 1, w, h)
while true do
  term.redirect(buffer)
  buffer.setVisible(false)
  term.clear()
  draw_things()
  buffer.redraw()
  buffer.setVisible(true)
  term.redirect(term.native())
end
better script?

local buffer = window.create(term.current(), 1, 1, w, h)
while true do
  term.redirect(buffer)
  buffer.setVisible(false)
  buffer.repositon(far_away_x, far_away_y)
  term.clear()
  draw_things()
  buffer.repositon(1, 1)
  buffer.redraw()
  buffer.setVisible(true) -- i think i can remove this if i dont see the far_away window
  term.redirect(term.native())
end
Edited on 06 August 2017 - 04:18 AM
KingofGamesYami #2
Posted 05 August 2017 - 07:52 PM
IIRC, the window API isn't exactly an optimized buffer. However, it should be sufficient for most purposes.
Bomb Bloke #3
Posted 06 August 2017 - 03:45 AM
It depends on what you want to use the buffer for. As terminal buffers go, the default "window" API is decently efficient these days, but with a specific use-case in mind it's just about always possible to make a purpose-built tool that's better for the job.

The loop you've got there would be better off with the redirect calls moved outside of it (redirect to your buffer before the loop starts, and redirect back to the old terminal if the loop ends), and with the redraw call removed entirely (setVisible(true) automatically redraws for you, and redraw does nothing while the buffer's still invisible).

Remember that term.redirect() returns the previous terminal object when you first call it, and when your script very first starts, it's unlikely that term.native() will be the current terminal. Record the old terminal when you redirect to your buffer so you can redirect back to that specific terminal as your script ends.