3 posts
Posted 10 January 2013 - 09:04 AM
What I need to do is:
function f1()
e,d,m = os.pullEvent("rednet_message")
print(m)
end
c = coroutine.create(f1)
coroutine.resume(c)
function f2()
sleep(1)
print("Do things")
f2()
end
f2()
but the pullEvent causes the coroutine to stop instantly, it wont even wait for the event to happen.
So how can I get this to work?
2088 posts
Location
South Africa
Posted 10 January 2013 - 09:14 AM
Use parallel api
function f1()
while true do
e, d, m = os.pullEvent("rednet_message")
print(m)
end
end
function f2()
while true do
print("Do things")
sleep(1)
end
end
parallel.waitForAny(f1, f2) – waitForAny will stop when any of them return, use waitForAll if you want it to only stop when all functions return/finish
2005 posts
Posted 10 January 2013 - 09:16 AM
You need to write the coroutine to yield, if you're going to use it as a coroutine.
If you're not experienced writing coroutines, then probably you should just use the parallel API.
3 posts
Posted 10 January 2013 - 09:26 AM
Use parallel api
function f1()
while true do
e, d, m = os.pullEvent("rednet_message")
print(m)
end
end
function f2()
while true do
print("Do things")
sleep(1)
end
end
parallel.waitForAny(f1, f2) – waitForAny will stop when any of them return, use waitForAll if you want it to only stop when all functions return/finish
I tried that and it messes up the f2 function when I trigger the rednet event (i have a for loop there)
3 posts
Posted 10 January 2013 - 09:28 AM
Nvm, it works now.
8543 posts
Posted 10 January 2013 - 10:05 AM
You need to write the coroutine to yield, if you're going to use it as a coroutine.
If you're not experienced writing coroutines, then probably you should just use the parallel API.
He did write it to yield. He just didn't write anything to resume it after the first time.