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

read() in codes?

Started by TheCursed_Donkey, 22 July 2012 - 07:55 PM
TheCursed_Donkey #1
Posted 22 July 2012 - 09:55 PM
HI! i just wondered if there was any way to use input for code?

like:

print ("Wich side is modem? : ")
(player writes "right")
print ("OK!")
and it will use that side?
Grim Reaper #2
Posted 22 July 2012 - 10:13 PM
Please, before you ask questions, take a look in the ComputerCraft Wiki or check tutorials.

However, I will answer your question: yes.

The standard 'read()' function is just that: standard in ComputerCraft.

It has one argument: an input mask.
This mask is useful if you want your password to be covered by a character like the asterisk ("*").

Here is an example using the code you provided:

print ("Wich side is modem? : ")
if read() == "right" then print( "OK!" )
else print( "Not OK!" ) end -- Or whatever you want.

It is also common practice to assign variable to read as to capture and store whatever 'read()' is returning for later use instead of just checking once in the if statement I showed.

Hope I helped! :)/>/>
Maome #3
Posted 23 July 2012 - 09:35 AM
Just to clarify you don't have to use if else statements if you are just choosing a modem side.

rednet.open(sString) takes a string so you can pass the input directly to that function as in

s = read()	 -- user inputs something like "derp"
rednet.open(s) -- runs as rednet.open("derp") ... which will fail but you get the point

Slightly more advanced:
Someone also provided this api which might be of interest to you. I've never used it, though.
Noodle #4
Posted 23 July 2012 - 11:26 AM
^^ Your code is correct. And yes, You do not need an if/else statement.