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?
8543 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
4 posts
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 :)/>
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 on 20 March 2014 - 03:20 PM
91 posts
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?