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

os.pullEvent for multiple, specific events

Started by Morzie, 19 March 2014 - 09:10 PM
Morzie #1
Posted 19 March 2014 - 10:10 PM
i'm trying to write a simple rednet chat program using 'os.pullEvent()', i'm trying to set it up so that it waits for the "char" event and the "rednet_message" event. The problem is that it pulls the "key" and "rednet_modem" events first, so is there a way to make it wait for multiple, specific events like 'os.pullEvent("char", "rednet_message")'? or is there a better way of doing all this?
Lyqyd #2
Posted 20 March 2014 - 12:17 AM

local event = {os.pullEvent()}
if event[1] == "char" then
  --char code here
elseif event[1] == "rednet_message" then
  --rednet code here
end
Morzie #3
Posted 20 March 2014 - 03:55 AM

local event = {os.pullEvent()}
if event[1] == "char" then
  --char code here
elseif event[1] == "rednet_message" then
  --rednet code here
end
Thank you, i'm not sure why it wasn't working before, but this works so thank you :)/>
Imred Gemu #4
Posted 20 March 2014 - 04:18 PM
You could also do something like this:

os.pullEventWhitelist = function(...)
local tArgs = {...}
while true do
  tEvent = {os.pullEvent()}
  if #tArgs < 1 then
   return unpack(tEvent)
  end
  for k, v in ipairs(tArgs) do
   if tEvent[1] == tostring(v)then
    return unpack(tEvent)
   end
  end
end
end
then call that like:

local event, par1, par2, par3 = os.pullEventWhitelist("key", "char")
Edited on 20 March 2014 - 03:20 PM
OReezy #5
Posted 20 March 2014 - 10:26 PM
You could also do something like this:

os.pullEventWhitelist = function(...)
local tArgs = {...}
while true do
  tEvent = {os.pullEvent()}
  if #tArgs < 1 then
   return unpack(tEvent)
  end
  for k, v in ipairs(tArgs) do
   if tEvent[1] == tostring(v)then
    return unpack(tEvent)
   end
  end
end
end
then call that like:

local event, par1, par2, par3 = os.pullEventWhitelist("key", "char")

Isn't this a bit redundant? Wouldn't you still have to check which type of event it was?