Lua beginner

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
luonline
Posts: 21
Joined: Thu May 15, 2008 1:57 pm

Lua beginner

#1 Post by luonline » Mon May 19, 2008 12:41 pm

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.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Lua beginner

#2 Post by Administrator » Mon May 19, 2008 12:59 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests