Jump to content




Reading User Input


12 replies to this topic

#1 MadCrayolaz

  • New Members
  • 39 posts

Posted 07 August 2012 - 12: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.

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 07 August 2012 - 12: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


#3 MadCrayolaz

  • New Members
  • 39 posts

Posted 07 August 2012 - 01:03 AM

View PostMysticT, on 07 August 2012 - 12:53 AM, said:

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?

#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 07 August 2012 - 01:06 AM

Oh, that's easier.
local input = read()
print("You entered: ", input)


#5 MadCrayolaz

  • New Members
  • 39 posts

Posted 07 August 2012 - 02:57 AM

Danke :P/>

#6 MadCrayolaz

  • New Members
  • 39 posts

Posted 07 August 2012 - 07: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>

#7 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 07 August 2012 - 07: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.

#8 MadCrayolaz

  • New Members
  • 39 posts

Posted 07 August 2012 - 07:58 PM

View PostMysticT, on 07 August 2012 - 07:41 PM, said:

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.

#9 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 07 August 2012 - 08:07 PM

Did you use write("text") or print("text")?

#10 MadCrayolaz

  • New Members
  • 39 posts

Posted 07 August 2012 - 08:18 PM

It was write, but after restarting the server, it seemed to fix. Thanks.

#11 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 08 August 2012 - 03:38 AM

Need any tips writing the program or have you made text adventures in console programs before?

#12 Rectorian

  • Members
  • 3 posts

Posted 12 October 2016 - 10:41 PM

What about storing numbers from read()?

#13 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 October 2016 - 12:39 AM

local yourVar = tonumber( read() )






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users