Jump to content




os.pullEvent() Question


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

#1 Skullblade

  • Members
  • 470 posts
  • LocationThe Big Apple, NY

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

#2 RunasSudo-AWOLindefinitely

  • Signature Abuser
  • 249 posts
  • Location/dev/earth1aus5

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 by RunasSudo, 20 January 2013 - 05:02 PM.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 05:02 PM

View PostRunasSudo, on 20 January 2013 - 05:00 PM, said:

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


#4 RunasSudo-AWOLindefinitely

  • Signature Abuser
  • 249 posts
  • Location/dev/earth1aus5

Posted 20 January 2013 - 05:06 PM

View PostTheOriginalBIT, on 20 January 2013 - 05:02 PM, said:

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.

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 05:16 PM

View PostRunasSudo, on 20 January 2013 - 05:06 PM, said:

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

View PostRunasSudo, on 20 January 2013 - 05:06 PM, said:

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...

View PostRunasSudo, on 20 January 2013 - 05:06 PM, said:

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...

#6 RunasSudo-AWOLindefinitely

  • Signature Abuser
  • 249 posts
  • Location/dev/earth1aus5

Posted 20 January 2013 - 05:56 PM

View PostTheOriginalBIT, on 20 January 2013 - 05:16 PM, said:

"mouse_click" has 4
The wiki lists 3... mouse_button, mouse_x and mouse_y.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 07:54 PM

View PostRunasSudo, on 20 January 2013 - 05:56 PM, said:

View PostTheOriginalBIT, on 20 January 2013 - 05:16 PM, said:

"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...

#8 Skullblade

  • Members
  • 470 posts
  • LocationThe Big Apple, NY

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.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 20 January 2013 - 08:26 PM

View PostRunasSudo, on 20 January 2013 - 05:56 PM, said:

View PostTheOriginalBIT, on 20 January 2013 - 05:16 PM, said:

"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

#10 RunasSudo-AWOLindefinitely

  • Signature Abuser
  • 249 posts
  • Location/dev/earth1aus5

Posted 20 January 2013 - 09:39 PM

View PostTheOriginalBIT, on 20 January 2013 - 05:16 PM, said:

"redstone" does not have any params, "key" has 1 event, "mouse_click" has 4

View PostTheOriginalBIT, on 20 January 2013 - 07:54 PM, said:

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.

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 09:43 PM

View PostRunasSudo, on 20 January 2013 - 09:39 PM, said:

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...

#12 RunasSudo-AWOLindefinitely

  • Signature Abuser
  • 249 posts
  • Location/dev/earth1aus5

Posted 20 January 2013 - 09:49 PM

View PostTheOriginalBIT, on 20 January 2013 - 09:43 PM, said:

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?

#13 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 21 January 2013 - 09:56 AM

View PostTheOriginalBIT, on 20 January 2013 - 05:02 PM, said:

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 by Eric, 21 January 2013 - 10:22 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users