Reputation: 244
I am just getting into Lua, and I am having issues with user input. No matter what I do, io.read() always returns nil.
This is what I have tried, nothing works.
name = io.read()
print(name)
name = io.read(7)
print(name)
print(io.read:())
All of these simply print nil.
I think what is happening is name
is set to io.read()
before the user has the chance to actually enter a value.
This:
while not name do name = io.read()
print name
simply causes the program to crash on a sort of short circuit, probably because the loop completes and the app refreshes within a fraction of a second.
I am using the wxLua environment for Windows, in the dialog version these programs run fine, but you obviously cannot do as much with that.
Upvotes: 2
Views: 362
Reputation: 26744
I'm not sure why you are trying io.read(7)
(as this would read 7 bytes), but the following script works for me with wxlua (although I use my own compiled wxlua library, which you can get here):
require('wx')
io.write("Enter something: ")
local value = io.read()
io.write(value, "\n")
Upvotes: 1