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

Do not wait for Event

Started by Patistar, 19 July 2012 - 07:58 AM
Patistar #1
Posted 19 July 2012 - 09:58 AM
Hello,

I found the ComputerCraft Mod a few days ago and I really like it. I'm writing and testing programs nearly the whole day, because it's a lot of fun. But today I got a problem, which I couldn't solve by myself.

I write a lot of programs, which shouldn't be terminated by ctrl + t. So I'm gonna put in: os.pullEvent = os.pullEventRaw .
But: I still want to be able to exit the program to remove bugs or things like that. So I got an idea: I wrote the following startup file:

sleep(1)
local sEvent, param = os.pullEvent()
if param == 56 then
  print("Bitte Kennwort eingeben:")
  pw = read("*")
  if( pw == "mypw" )
	return
  end
end
shell.run("doorlock")

I thought: Okay, it will take one second more to boot the computer and in this period of time I have the possibility to press a key and login as administrator and jump to the command line.

Some of you may have seen the problem already: os.pullEvent() will wait for an event. So I have to push every reboot a key (and also when I leave the server and reconnect).

Does anyone have an idea how to fix it? So how can I stop searching for an pullEvent after 1 second or how can I say that it should only look if there is an event but don't wait for one?


Thank you very much for your help. I hope you can understand the problem. If you want, you can also answer in German, because I'm from Germany. But English is, of course, also possible.


Patrick
Grim Reaper #2
Posted 19 July 2012 - 10:18 AM
Unfortunately, I only know about two semesters worth of German from my school and I didn't want to insult you with a garbage translation from Google Translate :P/>/>.

Try using a timer; a timer will trigger an event of it's own once the timeout is reached.

With this little bit of information you could check for a timer event (start the door lock) or a key event (debug mode).

Try something like this:


os.startTimer(1)
local sEvent, param = os.pullEvent()
if sEvent == "key" and param == 56 then
  print("Bitte Kennwort eingeben:")
  pw = read("*")
  if( pw == "mypw" )
        return
  else
print("Falsches Passwort. Fortsetzung..."); sleep(0.4)
  end
end
shell.run("doorlock")

The parameter for 'os.startTimer(n)' is the time in seconds before the timer event is triggered.

Hope I helped! :)/>/>
Patistar #3
Posted 19 July 2012 - 11:23 AM
Thank you very much, PaymentOption!
This solved perfectly my problem. It works fine. Found out that when I put: os.pullEvent = os.pullEventRaw in the startup file, I don't need it anymore in my door look program.

I'm indeed primal a few hours registered, but the forum has a nice impression to me. So I think I'll visit the forum more often :P/>/>


If anyone else want to use a startup like mine, here is the source:


os.pullEvent = os.pullEventRaw
local progname = "yourprogramsname" -- Name of the program you want to start after startup
local mypw = "yourpassword" -- Your password to get to command line
os.startTimer(1)
local sEvent, param = os.pullEvent()
if sEvent == "key" and param == 56 then
  print("Please type in your password:")
  pw = read("*")
  if pw == mypw then
    return
  else
    print("Password wrong. Continue...")
    sleep(0.5)
  end
end
shell.run( progname )
I hope I haven't put some errors in the code, because I've only copied (not by ctrl + c but by typing it) it.
(It's a little bit strange in English. If I copy it by a command and if I copy it by typing it, you use the same expression?! How can I express the difference?)