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

[Question][Lua] os.pullEvent(): Multiple Events

Started by newcat, 11 November 2012 - 03:43 AM
newcat #1
Posted 11 November 2012 - 04:43 AM
Hello,

short question: Is it possible to catch more than one event with os.pullEvent()?
I need that it fires when a rednet message comes in OR if an redstone-signal (in a bundled cable) comes.
Or is there any other solution for my problem (the solution shouldn't be very computionally expensive, because it's on a server and that program would be on about 5 PC's at the same time, and it must be in realtime so i can answer immediately on the two events)?

Hope you understand :unsure:/>/>

Thanks for your answer

newcat
Orwell #2
Posted 11 November 2012 - 04:53 AM
Only way I know of to do this:

while true do
  local event,p1,p2,p3 = os.pullEvent()
  if (event == 'redstone') then
    local side = p1
    -- stuff
  elseif (event == 'rednet_message') then
    local id = p1
    local msg = p2
    local dist = p3
    -- stuff
  end
end
newcat #3
Posted 11 November 2012 - 04:57 AM
Ah ok, I thought I must limit the os.pullEvent() to one event. Thank you!
Lyqyd #4
Posted 11 November 2012 - 07:15 AM
The parentheses in the if and elseif statements are unnecessary.
Orwell #5
Posted 11 November 2012 - 08:59 AM
The parentheses in the if and elseif statements are unnecessary.

Is that really worth creating a post for? :unsure:/>/> I know that, I'm used to doing so for readability and I'm also used to the c++ coding conventions.
I wonder if you just wanted to add something that some might not know, or that you think you're correcting me somehow?
Doyle3694 #6
Posted 11 November 2012 - 09:36 AM
He is helping ppl learn better. Use proper examples when you post code like that. because the guy who tries this might not know that, and then when he tells his buddie how to do this he says the parentheses are nessesary, and suddenly, 20 ppl come here and think they are nessesary. then what?
Orwell #7
Posted 11 November 2012 - 09:59 AM
Well, I believe using them is good practice. So that's what I'm teaching here. I asked Lyqyd if the purpose of his comment was teaching and clarification and if that's what it is, then it's all fine with me and I'll be happy to here that from him. But Doyle, I'm not obliged to post my code in a specific way just to teach every possible aspect of lua in 12 lines of code. Which isn't possible anyways:
because the guy who tries this might not know that, and then when he tells his buddie how to do this he says the parentheses are nessesary, and suddenly, 20 ppl come here and think they are nessesary. then what?
What if I don't use parentheses and the guy doesn't know you can use them, and then when he tells his buddy how to do this he says you can't use multiple conditions, and suddenly, 20 ppl come here and think they have to create an if statement for every condition. then what?

Seriously… We all have our ways, who are we to judge which is better?
Doyle3694 #8
Posted 11 November 2012 - 10:20 AM
what? ever heard of
 if condition1 and condition2 then
? That is the proper way to have many conditions
Orwell #9
Posted 11 November 2012 - 10:34 AM
Are we really gonna argue about semantics? You know damn well that there are many cases where parentheses are necessary for the order of precedence. You're totally avoiding the point.
I'm simply explaining something about events. I don't see how parentheses have anything to do with this, let alone my duty to not use them and the semantics of an expression (the multiple conditionals thing). Newcat has been helped, so can we please lock this thread and let it be? If you want to add anything, feel free to do so first.
Doyle3694 #10
Posted 11 November 2012 - 10:44 AM
No, under all my time of coding I have never, EVER, used parentheses in an if statement. please give an example of when it is necessary.
Orwell #11
Posted 11 November 2012 - 11:01 AM
http://www.lua.org/m...nual.html#2.5.6
And you're always going to find a way to write it without parentheses (https://en.wikipedia...Morgan%27s_laws), but it's about simplicity, readability and efficiency:
https://en.wikipedia...ki/Karnaugh_map (one way to simplify boolean equations I know of).
Once again you're missing the point, this is not some sort of show down of who's a better programmer. This is about helping the OP, which I've done. So can this thread be locked please?
Doyle3694 #12
Posted 11 November 2012 - 11:02 AM
stop commenting and the thread will drop down and die
Lyqyd #13
Posted 11 November 2012 - 02:01 PM
The parentheses in the if and elseif statements are unnecessary.

Is that really worth creating a post for? :unsure:/>/> I know that, I'm used to doing so for readability and I'm also used to the c++ coding conventions.
I wonder if you just wanted to add something that some might not know, or that you think you're correcting me somehow?

Yes, it is worth creating a post for. Parentheses have their place and it's useful to point out that they aren't necessary here, because there are places where they are necessary, even vital. Overuse of them hinders readability, though.

He is helping ppl learn better. Use proper examples when you post code like that. because the guy who tries this might not know that, and then when he tells his buddie how to do this he says the parentheses are nessesary, and suddenly, 20 ppl come here and think they are nessesary. then what?
What if I don't use parentheses and the guy doesn't know you can use them, and then when he tells his buddy how to do this he says you can't use multiple conditions, and suddenly, 20 ppl come here and think they have to create an if statement for every condition. then what?

Both of these are valid perspectives. However, this is not an instance wherein someone was putting out bad code (infinite loops through recursion or some such), but rather using an acceptable piece of syntax unnecessarily. It's not truly harmful, but it's good to make it known that it is, in fact, an unnecessary piece of syntax. There was no need to get after each other about this. Thread locked.