286 posts
Location
United States
Posted 30 January 2014 - 12:55 AM
I have figured out that this doesn't work, but why doesn't it work?
while true do
parallel.waitForAny(os.pullEvent("key"),os.pullEvent("rednet_message"))
print("Something Happened.")
end
I know I can work around this issue by setting up one pullEvent and then going through and testing the event. But I thought this would set up two "threads" and allow me to work off of whichever triggered first.
Edited on 30 January 2014 - 09:39 AM
1281 posts
Posted 30 January 2014 - 01:00 AM
I already explained this to apples, you can't pass any arguments directly to the functions you're running in parallel.
Edited on 30 January 2014 - 12:01 AM
286 posts
Location
United States
Posted 30 January 2014 - 01:08 AM
Okay. I actually simplified the code for purposes of the question. Here is my actual code – sorry if I caused confusion:
local event,message,sender,key
local function getRednetEvent()
sender,message,_ =os.pullEvent("rednet_message")
event="rednet"
end
local function getKeyEvent()
key,_ =os.pullEvent("key")
event = "key"
end
while true do
parallel.waitForAny(getKeyEvent(),getRednetEvent())
print (event)
end
I am not passing any arguments.
504 posts
Location
Seattle, WA
Posted 30 January 2014 - 01:11 AM
Your code doesn't work because the functions execute in the parameters of your call to the parallel API, meaning that you're calling the parallel API with whatever is returned by those functions. You have to actually pass the functions themselves to the parallel API so that it can create threads out of them and run them for you.
So, you can change that parallel line to this:
parallel.waitForAny (getKeyEvent, getRednetEvent)
Also, the "rednet_message" event no longer exists, if I'm not mistaken. If you want to handle communications over rednet, you'll have to use "modem_message" to capture modem messages as that is the way rednet works now. You can still use the rednet functions, but they are actually using "modem_message" underneath.
Edited on 30 January 2014 - 12:13 AM
7508 posts
Location
Australia
Posted 30 January 2014 - 01:12 AM
Okay so this is a perfect
thread for you, particularly
my reply.
also it should be noted that Coroutines do not run at the same time, so this
But I thought this would set up two "threads" and allow me to work off of whichever triggered first.
may not have the intention you're hoping for. actually its a pointless use of coroutines, just setup an event loop that checks the events.
Also, the "rednet_message" event no longer exists, if I'm not mistaken. If you want to handle communications over rednet, you'll have to use "modem_message" to capture modem messages as that is the way rednet works now. You can still use the rednet functions, but they are actually using "modem_message" underneath.
You are mistaken, the
rednet_message event still exists for backward compatibility, there is a rednet coroutine running with the shell that translates
modem_message into
rednet_message
Edited on 30 January 2014 - 12:16 AM
1281 posts
Posted 30 January 2014 - 01:15 AM
Ditch the brackets in the parallel function calls. You don't want to actually call them, just pass them as variables.
Edit: much ninja, so skill
Edited on 30 January 2014 - 12:16 AM
286 posts
Location
United States
Posted 30 January 2014 - 01:16 AM
You folks are awesome, and I am an idiot. I get it. By using getKeyEvent() with parenthesis, I was not passing a pointer to a function. By removing the parenthesis, I pass a pointer. Fixed that, fixed the code.
[SOLVED] with the help of an amazing coding community
Edited on 30 January 2014 - 12:17 AM