Jump to content




Timer Help


  • You cannot reply to this topic
2 replies to this topic

#1 mrdawgza

  • Members
  • 110 posts
  • LocationSouth Africa

Posted 29 September 2013 - 07:07 PM

Hello, I am still working on my OS.

Now, I am busy recreating my hole OS,
so I started with startup, now
I load my custom APIs in startup, and I want to make it so that if I push S during startup, or
for the first five seconds of startup, it'll quit the startup and return to shell.

(I figured it has something to do with a timer or something)

I am working on a little code, here it is:

(I used any key just to make things easy and simple for testing)
shell.run("clear")
print("Press any key to hear a secret.")
os.startTimer(3)
if os.pullEvent("key") then
  print("ComputerCraft is awesome.")
else
os.pullEvent(timer)

end

The problem:

I want it to time down whilst listening for a key input, so then if you don't push
any key, it'll return to shell, but if you do push a key it'll show the secret.
But on that little code, it'll display the secret on key press, but it won't
return to shell as if I never pushed a key,


Am I stupid meaning it has nothing to do with timers or is there a problem?

(I will give the person who helped me most credit in my OS for helping me if the problem is resolved)

#2 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 29 September 2013 - 07:09 PM

print("Press any key to hear a secret")
timerID = os.startTimer(3)
while true do
  event, parameter = os.pullEvent()
  if event == "timer" and parameter == timerID then
    pressedAKey = false
    break
  elseif event == "key" then
    pressedAKey = true
    break
  end
end

if pressedAKey then
  -- they pressed a key
else
  -- they didn't press a key
end


#3 mrdawgza

  • Members
  • 110 posts
  • LocationSouth Africa

Posted 29 September 2013 - 07:22 PM

Thank you. Topic is now probably solved.
Credited immibis.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users