Jump to content




os.pullEvent for multiple, specific events


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

#1 Morzie

  • Members
  • 4 posts

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?

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#3 Morzie

  • Members
  • 4 posts

Posted 20 March 2014 - 03:55 AM

View PostLyqyd, on 20 March 2014 - 12:17 AM, said:

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 :)

#4 Imred Gemu

  • Members
  • 47 posts

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 by Imred Gemu, 20 March 2014 - 04:20 PM.


#5 OReezy

  • Members
  • 91 posts

Posted 20 March 2014 - 10:26 PM

View PostImred Gemu, on 20 March 2014 - 04:18 PM, said:

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users