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

Read from screen and use functions

Started by Pippin_, 21 January 2013 - 05:40 AM
Pippin_ #1
Posted 21 January 2013 - 06:40 AM
Hi!
I'm trying to write a program for a turtle that will write words with blocks. I want to have a input system where you type in the words and the turtle simply write them with blocks.

I think i'm going to write functions for every character but if anyone have a better idea please tell me.
The problem i'm having is that i don't know how to write the input system so some help wold be great.

Thanks a lot!
SuicidalSTDz #2
Posted 21 January 2013 - 06:43 AM
That would be the best solution I can think of. Have every letter as a function, then with user input it will read that function and build each letter

Edit: See below comment
Mads #3
Posted 21 January 2013 - 07:05 AM
Or you could construct a table like this:


local letterData {
    a = somedata,
    b = someotherdata,
    etc.
}

Then you would draw it like this:


local function drawLetter(letter) 
    local data = letterData[letter]

    -- Draw data
end
SuicidalSTDz #4
Posted 21 January 2013 - 07:08 AM
Or you could construct a table like this:


local letterData {
	a = somedata,
	b = someotherdata,
	etc.
}

Then you would draw it like this:


local function drawLetter(letter)
	local data = letterData[letter]

	-- Draw data
end
This is a better solution. Much more compact and easier to understand. Also beats scrolling through 26 functions ;)/>
Pippin_ #5
Posted 21 January 2013 - 07:16 AM
I did not understand how the input will tell which function to run. Can someone explain pretty basic i'm kind of new to lua.
SuicidalSTDz #6
Posted 21 January 2013 - 07:43 AM
I did not understand how the input will tell which function to run. Can someone explain pretty basic i'm kind of new to lua.

So:
if read() == (letter) then
[build the letter that the user typed]
end

Example:
input = read()
if input == "a" then
a()
end

Just a rough example, the function a() will contain the code the turtle will run to build the letter A
Pippin_ #7
Posted 21 January 2013 - 08:04 AM
I did not understand how the input will tell which function to run. Can someone explain pretty basic i'm kind of new to lua.

So:
if read() == (letter) then
[build the letter that the user typed]
end

Example:
input = read()
if input == "a" then
a()
end

Just a rough example, the function a() will contain the code the turtle will run to build the letter A

Yes but i want to type in a word, not a letter.
Bubba #8
Posted 21 January 2013 - 08:07 AM
In that case, just do something like the following:

local input = read()
local letters = {} --A table which will contain each individual character
for i=1,#input do
  letters[i] = string.sub(input, i, i) -- Basically just gets the individual character at the index i and puts it into the table
end

for i=1, #letters do
  build(letters[i]) --For each of the letters, run the build function
end

The above code of course assumes that you have a build function.

Tables are probably the best way to handle the whole building part. In fact, considering that you can have functions inside of tables, something like this may be the easiest.


local build = {
  ["a"] = function()
   --dostuff
  end;
  ["b"] = function()
   --do other stuff
  end;
}


And then to build the letters, you would just do something like build.a() or build["a"]().

In the context of having a table of characters like in my first example, you would just change it to this:

local input = read()
local letters = {} --A table which will contain each individual character
for i=1,#input do
  letters[i] = string.sub(input, i, i) -- Basically just gets the individual character at the index i and puts it into the table
end

for i=1, #letters do
  build[letters[i]]()--For each of the letters, run the build function
end
ChunLing #9
Posted 21 January 2013 - 02:39 PM
Why not just combine those two loops?
for i=1,#input do
  build[string.sub(input, i, i)]()
end
Just a readability issue, or were you going to use the table letters for something else?