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

Question about the term API

Started by Robd, 18 June 2012 - 07:55 PM
Robd #1
Posted 18 June 2012 - 09:55 PM
[Sorry for the rather vague tittle, this was a hard problem to properly label]

I recently started work on an OS which makes use of the arrow keys in place of a mouse. I have the mouse setup, working quite well actually. However I have a rather major problem and I have no idea what I can do to fix it, while keeping the OS as dynamic as possible.


The mouse is run by writing a single symbol (^) in a given set of coordinates. When the mouse is moved, the up-carrot is replace with a space. This works great, until you move the mouse over an existing piece of text, in which case, upon being moved, the mouse will erase the text.

I was wondering if there was any way to "read" from the screen what letter is there, save it as a variable, and then place THAT instead of a space, thereby eliminating the problem. Any other suggestions would be equally helpful.
MysticT #2
Posted 18 June 2012 - 10:02 PM
There's no way to read what's already on the screen, but you could have a "buffer" to store the text, and then write it to the screen. I use that method for my os, using a table with each line of text.
Robd #3
Posted 18 June 2012 - 10:25 PM
I was afraid of that… I guess I've got some work to do. Thanks for the help
kazagistar #4
Posted 19 June 2012 - 06:46 PM
Buffered output is generally useful. Personally, I am trying to figure out how to set up multiple buffers on the same screen. Or I will be once my current problems with automated turtles are solved, which might yet be a while…
Robd #5
Posted 19 June 2012 - 10:41 PM
Would either of you have much of a suggestion for creating a multi dimensional array which I could save individual characters on by coordinate? I'm not really sure how to go about making a buffer to store that.
MysticT #6
Posted 19 June 2012 - 10:49 PM
You can use this function:

local function createBuffer(w, h)
  local buff = {}
  for y = 1, h do
    buff[y] = {}
    for x = 1, w do
      buff[y][x] = " " -- set empty character
    end
  end
  return t
end
Then, you set the characters like:

local buff = createBuffer(term.getSize()) -- create the buffer
local x, y = term.getCursorPos()
buff[y][x] = "X" -- important: y first (buff[y][x], not buff[x][y])