This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
os.pullEvent() Question
Started by Skullblade, 20 January 2013 - 03:59 PMPosted 20 January 2013 - 04:59 PM
I was wondering if it was possible to restrict os.pullEvent to two different parameters rather then just 1. So the os.pullEvent would be fired by one or the other but no other ones. Any help would be great thanks
Posted 20 January 2013 - 05:00 PM
Call it with no arguments and check the first return thingy.
while true do
local event, p1, p2, p3 = os.pullEvent()
if event == "a" then
--do event a stuff
break
elseif event == "b" then
--do event b stuff
break
end
end
Edited on 20 January 2013 - 04:02 PM
Posted 20 January 2013 - 05:02 PM
This is what he meansCall it with no arguments and check the first return thingy.
local event = { os.pullEvent() }
if event[1] == "key" then
local keyCode = event[2]
elseif event[1] == "char" then
local charPressed = event[2]
end
Posted 20 January 2013 - 05:06 PM
I have just discovered an up-side to my anti-getting-ninja'd technique of posting a summary first and then editing to add more information.This is what he meanslocal event = { os.pullEvent() } if event[1] == "key" then local keyCode = event[2] elseif event[1] == "char" then local charPressed = event[2] end
It makes other people look silly when they re-state what I edited in. :D/>
On topic: Why did you os.pullEvent() to a table? Seems rather superfluous…
Also, your code will continue the program whether or not a valid event was fired.
Posted 20 January 2013 - 05:16 PM
I'm not too sure why I look silly there… I wasn't the one checking for events "a" and "b" :P/>I have just discovered an up-side to my anti-getting-ninja'd technique of posting a summary first and then editing to add more information.
It makes other people look silly when they re-state what I edited in. :D/>
Because it makes it easier when dealing with different events that have different params… for example "redstone" does not have any params, "key" has 1 event, "mouse_click" has 4… so thisOn topic: Why did you os.pullEvent() to a table? Seems rather superfluous…
local event, p1, p2, p3, p4 = os.pullEvent()
looks much neater like this
local event = { os.pullEvent() }
And your not loosing any data, its just in a table… also I've found some ppl get confused when there is 4 params declared but they are only using 1 for the thing… in a table, they use how many they want…I was just expanding on your dodgy "first draft" post…Also, your code will continue the program whether or not a valid event was fired.
Posted 20 January 2013 - 05:56 PM
The wiki lists 3… mouse_button, mouse_x and mouse_y."mouse_click" has 4
Posted 20 January 2013 - 07:54 PM
4… event, mouse_button, mouse_x, mouse_yThe wiki lists 3… mouse_button, mouse_x and mouse_y."mouse_click" has 4
an event is returned from pullEvent therefore there is 4 return values…
Posted 20 January 2013 - 08:14 PM
Thx for the help guys:D didn't do it like the way you showed me but it got me on the right track. I put the os.pullEvent in a while true loop and if it was one of the correct pullEvents it did stuff and broke. then it was essentially "pulling" one thing or the other but no other events; with yours it would pull the event and if it wasn't one of the specified events it would just continue on with the other code.
Posted 20 January 2013 - 08:26 PM
The wiki lists 3… mouse_button, mouse_x and mouse_y."mouse_click" has 4
os.pullEvent() parameters start from the second variable. So event + 3 params = 4
Posted 20 January 2013 - 09:39 PM
"redstone" does not have any params, "key" has 1 event, "mouse_click" has 4
Oh dear. It would appear you are using inconsistent terminology.an event is returned from pullEvent therefore there is 4 return values…
"redstone" has 0, "key" has 1, "mouse_click" has 3 OR
"redstone" has 1, "key" has 2, "mouse_click" has 4.
Posted 20 January 2013 - 09:43 PM
Eh… I'm only human… I can make mistakes and/or change my mind half way through typing stuff… Every one makes mistakes…Oh dear. It would appear you are using inconsistent terminology.
"redstone" has 0, "key" has 1, "mouse_click" has 3 OR
"redstone" has 1, "key" has 2, "mouse_click" has 4.
Posted 20 January 2013 - 09:49 PM
Well, I certainly don't! I'm far 太小心,这个事n'est pas possible!Eh… I'm only human… I can make mistakes and/or change my mind half way through typing stuff… Every one makes mistakes…
Now what was I saying again?
Posted 21 January 2013 - 09:56 AM
This is what he meanslocal event = { os.pullEvent() } if event[1] == "key" then local keyCode = event[2] elseif event[1] == "char" then local charPressed = event[2] end
Really, you should requeue the event if you didn't handle it, shouldn't you?
local event = {os.pullEvent()}
if event[1] == "key" then
local keyCode = event[2]
elseif event[1] == "char" then
local charPressed = event[2]
else
os.queueEvent(unpack(event))
end
EDIT: No, you definitely should not! The parallel API makes sure that you don't swallow messages expected by other coroutines.
Edited on 21 January 2013 - 09:22 AM