Page 1 of 1

Lua beginner

Posted: Mon May 19, 2008 12:41 pm
by luonline
Hi!
Following script doesn't work, because it doesn't wait for user input:

Code: Select all

startKey = key.VK_INSERT;
stopKey = key.VK_DELETE;
function main()
  io.write("Are you OK? (Y/N): ");
  answer = io.read(1);
--  answer = io.read("*line");
  print("Your choice is:", answer);
end
startMacro(main);
Output is:

Code: Select all

MicroMacro v0.97
SolarImpact
http://solarimpact.servegame.com


Please enter the script name to run.
Type in 'exit' (without quotes) to exit.
Script> entrada.lua
Opening entrada.lua...

Starting script execution - Press CTRL+C to exit.
Press CTRL+L to cancel execution and load a new script.
-------------------------------------------------
The macro is currently not running. Press the start key (Insert) to begin.
You may use (Delete) key to stop/pause the script.
Started.
Are you OK? (Y/N): Your choice is:

Stopping execution.
Where am I going wrong? I trying to make a simple menu to users...
Thanks in advance.

Re: Lua beginner

Posted: Mon May 19, 2008 12:59 pm
by Administrator
Actually, this reminds me of when I was writing a command-line driven FTP-ish server/client using MicroMacro... Yes, I was somewhat successful: the server ran fine, accepted multiple users, and transferring files between client/server worked great, even with binary files. Then I realized that there was absolutely no need for it, and it was a big waste of time...but fun.

Anyways, most of your code is correct. The problem is that the keyboard buffer still has information in it from before. What you need to do is completely empty the buffer just before trying to accept input, that way you can be certain that prior input will not screw with your current input.

Try this:

Code: Select all

function main()
  io.write("Are you OK? (Y/N): ");
  io.stdin:flush();
  local answer = io.stdin:read(1);

  print("Your choice is:", answer);
end
the "stdin" is optional, so I put it in anyways. As well as the local. If you don't know what "local" does, go ahead and look it up in the Lua manual.