Posted 21 April 2014 - 02:45 AM
To put it simply, how do I use OS.pullEvent without pausing the program? I need to check if the user has pressed a key at any time, while also updating information.
local YIELD_TIME = 0.05 -- The amount of time we yield to wait for key presses. Real short to look like its continuous.
local function updateStuff()
while isThereStuffToDo() do
doStuff()
coroutine.yield()
end
end
local function handleEventData (eventData)
handleTheStuff (unpack (eventData))
end
local updateThread = coroutine.create (updateStuff)
while coroutine.status (updateThread) ~= "dead" do
local timer = os.startTimer (YIELD_TIME)
local eventData = { os.pullEvent() }
-- Handle the event data if we didn't catch our yield timer.
-- NOTE: The timer might get caught by the 'handleTheStuff' function if it pulls an event fast enough.
if not (eventData[1] == "timer" and eventData[2] == timer) then
handleEventData (eventData)
end
-- Update our information now that events have been handled.
updateStuff()
end
That's a bad idea. You should only queue a new timer event when you get the old one, or you'll have a huge pileup of timer events, with the pile getting one timer bigger every non-dummy-timer event.For example, you could do something like this:local YIELD_TIME = 0.05 -- The amount of time we yield to wait for key presses. Real short to look like its continuous. local function updateStuff() while isThereStuffToDo() do doStuff() coroutine.yield() end end local function handleEventData (eventData) handleTheStuff (unpack (eventData)) end local updateThread = coroutine.create (updateStuff) while coroutine.status (updateThread) ~= "dead" do local timer = os.startTimer (YIELD_TIME) local eventData = { os.pullEvent() } -- Handle the event data if we didn't catch our yield timer. -- NOTE: The timer might get caught by the 'handleTheStuff' function if it pulls an event fast enough. if not (eventData[1] == "timer" and eventData[2] == timer) then handleEventData (eventData) end -- Update our information now that events have been handled. updateStuff() end
local YIELD_TIME = 0.05 -- The amount of time we yield to wait for key presses. Real short to look like its continuous.
local function updateStuff()
while isThereStuffToDo() do
doStuff()
coroutine.yield()
end
end
local function handleEventData (eventData)
handleTheStuff (unpack (eventData))
end
local updateThread = coroutine.create (updateStuff)
local timer = os.startTimer (YIELD_TIME)
while coroutine.status (updateThread) ~= "dead" do
local eventData = { os.pullEvent() }
-- Handle the event data if we didn't catch our yield timer.
-- NOTE: The timer might get caught by the 'handleTheStuff' function if it pulls an event fast enough.
if eventData[1] == "timer" and eventData[2] == timer then
timer = os.startTimer (YIELD_TIME)
else
handleEventData (eventData)
end
-- Update our information now that events have been handled.
updateStuff()
end
That's a bad idea. You should only queue a new timer event when you get the old one, or you'll have a huge pileup of timer events, with the pile getting one timer bigger every non-dummy-timer event.For example, you could do something like this:local YIELD_TIME = 0.05 -- The amount of time we yield to wait for key presses. Real short to look like its continuous. local function updateStuff() while isThereStuffToDo() do doStuff() coroutine.yield() end end local function handleEventData (eventData) handleTheStuff (unpack (eventData)) end local updateThread = coroutine.create (updateStuff) while coroutine.status (updateThread) ~= "dead" do local timer = os.startTimer (YIELD_TIME) local eventData = { os.pullEvent() } -- Handle the event data if we didn't catch our yield timer. -- NOTE: The timer might get caught by the 'handleTheStuff' function if it pulls an event fast enough. if not (eventData[1] == "timer" and eventData[2] == timer) then handleEventData (eventData) end -- Update our information now that events have been handled. updateStuff() end
local YIELD_TIME = 0.05 -- The amount of time we yield to wait for key presses. Real short to look like its continuous. local function updateStuff() while isThereStuffToDo() do doStuff() coroutine.yield() end end local function handleEventData (eventData) handleTheStuff (unpack (eventData)) end local updateThread = coroutine.create (updateStuff) local timer = os.startTimer (YIELD_TIME) while coroutine.status (updateThread) ~= "dead" do local eventData = { os.pullEvent() } -- Handle the event data if we didn't catch our yield timer. -- NOTE: The timer might get caught by the 'handleTheStuff' function if it pulls an event fast enough. if eventData[1] == "timer" and eventData[2] == timer then timer = os.startTimer (YIELD_TIME) else handleEventData (eventData) end -- Update our information now that events have been handled. updateStuff() end