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

pullEvent stops coroutine

Started by erkkiqaz, 10 January 2013 - 08:04 AM
erkkiqaz #1
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?
remiX #2
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
ChunLing #3
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.
erkkiqaz #4
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)
erkkiqaz #5
Posted 10 January 2013 - 09:28 AM
Nvm, it works now.
Lyqyd #6
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.