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

Read 'segments' from table

Started by Semx11, 08 December 2013 - 09:04 AM
Semx11 #1
Posted 08 December 2013 - 10:04 AM
Hello everybody,

I'd like to know how you can read 'segments' from tables.
With that, I mean the following:

Lets say that I have this table (called image):


local image = {
"XXXOXXXOXXX",
"XOOOOXOOXOX",
"XXXOOXOOXXX",
"XOOOOXOOXOO",
"XOOOOXOOXOO"
}

Using this table, I want it to read the lines, character by character. (Maybe by using term.readLine() or something?)

X = term.setBackgroundColor(colors.white) term.write(" ") == So this will be a white 'pixel'
O = term.setBackgroundColor(colors.black) term.write(" ") == And this will just be a space (or a 'black pixel')

If you're good at guessing, you maybe already figured out that I want to use a table to 'pixel' a certain image on a screen, using a table.
For this one I only want white 'pixels' and spaces, but maybe for future things I'd like to use it (by using multiple different characters) to print whole 'coloured images'.

I have no clue how to make it do this.
So is there anybody around here that knows how you can do this?
If so, can you help me? C:

Kind regards,

- Semx11

Note: I know that this is possible, because nitrogenfingers also used this in one of his programs, called "goldrunner". In that game he uses a table with characters (like above) to make the program 'write' a level.
apemanzilla #2
Posted 08 December 2013 - 10:53 AM
Not sure I quite get what you're trying to do - read a part of a value of a table?

Anyway, if you want to read it one character at a time, this should work:

function readCharFromTable( tbl, key, character )
  return string.sub(tbl[key],character,character)
end
Not too difficult.