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

Reading User Input

Started by MadCrayolaz, 06 August 2012 - 11:46 PM
MadCrayolaz #1
Posted 07 August 2012 - 01:46 AM
How would I go about making a program I am writing recieve user input through typing?

The context of this is for an RPG that requires knowing if a user types up down left right, etc.
MysticT #2
Posted 07 August 2012 - 01:53 AM
You mean using key presses? You need to use events. Here's a simple example:

while true do
  local evt, key = os.pullEvent("key")
  print("Key ", key, " pressed")
end
MadCrayolaz #3
Posted 07 August 2012 - 02:03 AM
You mean using key presses? You need to use events. Here's a simple example:

while true do
  local evt, key = os.pullEvent("key")
  print("Key ", key, " pressed")
end

I'll give a different example.

I am also working on a login system.
I want to promp the user to enter their username.
How would I do that?
MysticT #4
Posted 07 August 2012 - 02:06 AM
Oh, that's easier.

local input = read()
print("You entered: ", input)
MadCrayolaz #5
Posted 07 August 2012 - 03:57 AM
Danke :P/>/>
MadCrayolaz #6
Posted 07 August 2012 - 08:39 PM
Another thing, if I wanted there to be text prefacing the place to type, how would I do that?
I want it to look like this:
Username: <entered text>
MysticT #7
Posted 07 August 2012 - 08:41 PM
Using write:

write("Username: ")
local username = read()
You should check out the wiki to see what functions are in CC and what they do.
MadCrayolaz #8
Posted 07 August 2012 - 08:58 PM
Using write:

write("Username: ")
local username = read()
You should check out the wiki to see what functions are in CC and what they do.

That's just it, I did that, but it put it on a new line, and I didn't specify a new line char.
Cranium #9
Posted 07 August 2012 - 09:07 PM
Did you use write("text") or print("text")?
MadCrayolaz #10
Posted 07 August 2012 - 09:18 PM
It was write, but after restarting the server, it seemed to fix. Thanks.
Pharap #11
Posted 08 August 2012 - 04:38 AM
Need any tips writing the program or have you made text adventures in console programs before?
Rectorian #12
Posted 12 October 2016 - 11:41 PM
What about storing numbers from read()?
Bomb Bloke #13
Posted 13 October 2016 - 01:39 AM
local yourVar = tonumber( read() )