This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Bandus's profile picture

[Question] Event Loop

Started by Bandus, 03 April 2012 - 02:26 AM
Bandus #1
Posted 03 April 2012 - 04:26 AM
I am baffled as to why this won't work. I've checked out Casper's tutorial on events as well as read some on lua events, however, I feel like I've done this code correctly. Basically, it responds to the "rednet_message" event. But, when one is at the local machine and presses "1" it indicates it is an invalid selection. Is it the order of the events?



while true do
event, p1, p2 = os.pullEvent()

if event == "rednet_message" and p2 == "water" then
  waterremote()
end

if event == "char" and p1 == "1" then
   water()
  else
  print("Invalid Selection!")
  sleep(3)
  shell.run("startup")
end
end
Advert #2
Posted 03 April 2012 - 06:34 AM
Moved to Ask a Pro.

Please post in the correct forum next time.

You're getting the invalid selection message because every char event also fires a "key" event before it.
Once your computer gets the key event, it'll sleep, ignoring the "char" event that follows.

You can put the check for p1 inside the if event == "char" then … end statement, and it should work fine.

I don't really see why you want to restart the computer upon hitting a wrong key, though, you could indicate that a wrong key has been pressed, instead.