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

os.pullEvent() Question

Started by Skullblade, 20 January 2013 - 03:59 PM
Skullblade #1
Posted 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
RunasSudo-AWOLindefinitely #2
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
theoriginalbit #3
Posted 20 January 2013 - 05:02 PM
Call it with no arguments and check the first return thingy.
This is what he means

local event = { os.pullEvent() }

if event[1] == "key" then
  local keyCode = event[2]
elseif event[1] == "char" then
  local charPressed = event[2]
end
RunasSudo-AWOLindefinitely #4
Posted 20 January 2013 - 05:06 PM
This is what he means

local event = { os.pullEvent() }

if event[1] == "key" then
  local keyCode = event[2]
elseif event[1] == "char" then
  local charPressed = event[2]
end
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/>

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.
theoriginalbit #5
Posted 20 January 2013 - 05:16 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.
It makes other people look silly when they re-state what I edited in. :D/>
I'm not too sure why I look silly there… I wasn't the one checking for events "a" and "b" :P/>

On topic: Why did you os.pullEvent() to a table? Seems rather superfluous…
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 this

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…

Also, your code will continue the program whether or not a valid event was fired.
I was just expanding on your dodgy "first draft" post…
RunasSudo-AWOLindefinitely #6
Posted 20 January 2013 - 05:56 PM
"mouse_click" has 4
The wiki lists 3… mouse_button, mouse_x and mouse_y.
theoriginalbit #7
Posted 20 January 2013 - 07:54 PM
"mouse_click" has 4
The wiki lists 3… mouse_button, mouse_x and mouse_y.
4… event, mouse_button, mouse_x, mouse_y

an event is returned from pullEvent therefore there is 4 return values…
Skullblade #8
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.
remiX #9
Posted 20 January 2013 - 08:26 PM
"mouse_click" has 4
The wiki lists 3… mouse_button, mouse_x and mouse_y.

os.pullEvent() parameters start from the second variable. So event + 3 params = 4
RunasSudo-AWOLindefinitely #10
Posted 20 January 2013 - 09:39 PM
"redstone" does not have any params, "key" has 1 event, "mouse_click" has 4
an event is returned from pullEvent therefore there is 4 return values…
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.
theoriginalbit #11
Posted 20 January 2013 - 09:43 PM
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.
Eh… I'm only human… I can make mistakes and/or change my mind half way through typing stuff… Every one makes mistakes…
RunasSudo-AWOLindefinitely #12
Posted 20 January 2013 - 09:49 PM
Eh… I'm only human… I can make mistakes and/or change my mind half way through typing stuff… Every one makes mistakes…
Well, I certainly don't! I'm far 太小心,这个事n'est pas possible!
Now what was I saying again?
Eric #13
Posted 21 January 2013 - 09:56 AM
This is what he means

local 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