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

Pulling an event for a certain amount of time

Started by lieudusty, 02 November 2012 - 03:36 PM
lieudusty #1
Posted 02 November 2012 - 04:36 PM
Hi everyone! :D/>/>

I'm making a program and I need it to only do os.pullEvent() for a certain amount of time (2 seconds). Is there anyway to do this considering os.pullEvent freezes the computer until an event occurs? Thanks
Pharap #2
Posted 02 November 2012 - 04:39 PM
If you do os.startTimer() beforehand, the os.pullEvent() will grab the timer event after that set time period if no events are sent before the timer event fires.

So:

local eventtocatch = 'key' --(for example)
local timeout = os.startTimer(2)
local e,arg1,arg2,arg3 = os.pullEvent()
if e=='timer' and arg1==timeout then
--time up
elseif e==eventtocatch then
--execute other code
end
lieudusty #3
Posted 02 November 2012 - 05:00 PM
If you do os.startTimer() beforehand, the os.pullEvent() will grab the timer event after that set time period if no events are sent before the timer event fires.

So:

local eventtocatch = 'key' --(for example)
local timeout = os.startTimer(2)
local e,arg1,arg2,arg3 = os.pullEvent()
if e=='timer' and arg1==timeout then
--time up
elseif e==eventtocatch then
--execute other code
end
Thank you very much! This was simpler than I thought ^-^
Pharap #4
Posted 03 November 2012 - 08:36 PM
You're welcome. I've recently been working quite closely with os.pullEvent so this was something I practically jumped on lol