The context of this is for an RPG that requires knowing if a user types up down left right, etc.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Reading User Input
Started by MadCrayolaz, 06 August 2012 - 11:46 PMPosted 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.
The context of this is for an RPG that requires knowing if a user types up down left right, etc.
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
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?
Posted 07 August 2012 - 02:06 AM
Oh, that's easier.
local input = read()
print("You entered: ", input)
Posted 07 August 2012 - 03:57 AM
Danke :P/>/>
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>
I want it to look like this:
Username: <entered text>
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.Posted 07 August 2012 - 08:58 PM
Using write:You should check out the wiki to see what functions are in CC and what they do.write("Username: ") local username = read()
That's just it, I did that, but it put it on a new line, and I didn't specify a new line char.
Posted 07 August 2012 - 09:07 PM
Did you use write("text") or print("text")?
Posted 07 August 2012 - 09:18 PM
It was write, but after restarting the server, it seemed to fix. Thanks.
Posted 08 August 2012 - 04:38 AM
Need any tips writing the program or have you made text adventures in console programs before?
Posted 12 October 2016 - 11:41 PM
What about storing numbers from read()?
Posted 13 October 2016 - 01:39 AM
local yourVar = tonumber( read() )