coroutine doesn't working

Hi,

I have just copy the below example from the tutorial about coroutine and it give me error one it is excute.

What am I missing here ?

The error getting is : “attempt to yield across metamethod/C-call boundary”

https://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

function WaitUntilPropertyTrue( object, name, update ) while not object[name] do if update then update() end coroutine.yield() end end

local rect = display.newRect( 100, 100, 50, 50 ) rect.isVisible = false timer.performWithDelay( 5000, function() rect.isVisible = true end ) WaitUntilPropertyTrue( rect, "isVisible" ) --DoSomethingElse() -- This code should be excute 5 sec after the above line code

Take a look at this tutorial: http://www.lua.org/pil/9.1.html

You need to instantiate a coroutine:

co = coroutine.create(function () print("test 1"); coroutine.yield(); print("test 2"); end); coroutine.resume(co);

Note, I didn’t test the code above, so you may need to fiddle with it.

As @ apucciani says, it needs to be inside a coroutine. By that point in the article I was trying not to re-include everything in each snippet, but you should use something like the CoroPerformWithDelay () function defined earlier in the article.

I was trying what you suggested and read the article. Using the Create/Resume and all other but nothing works…

Can you provide the exact code change that need to take place?

Tnx

 Yuval

Hi.

I did something like it (a few weeks before I wrote the article) on the Corona Geek show, and it looks like I left it in that state, if you want to try that: coroutines sample

That said, this is what the full code would look like:

function CoroPerformWithDelay( delay, func, n ) local wrapped = coroutine.wrap( function( event ) func( event ) -- Do the action... return "cancel" -- ...then tell the timer to die when we're done. end ) local event2 -- Our "shadow" event. return timer.performWithDelay( delay, function( event ) event2 = event2 or { source = event.source } -- On first run, save source... event2.count = event.count -- ...update these every time. event2.time = event.time local result = wrapped( event2 ) -- Update the coroutine. It will pick up the event on the first run. if result == "cancel" then timer.cancel( event2.source ) -- After func completes, or on a cancel request, kill the timer. end end, n or 0 ) end function WaitUntilPropertyTrue( object, name, update ) while not object[name] do if update then update() end coroutine.yield() end end function DoSomethingElse () print("DONE!") end CoroPerformWithDelay(20, function() local rect = display.newRect( 100, 100, 50, 50 ) rect.isVisible = false timer.performWithDelay( 5000, function() rect.isVisible = true end ) WaitUntilPropertyTrue( rect, "isVisible" ) DoSomethingElse() -- This code should be excute 5 sec after the above line code end)

Take a look at this tutorial: http://www.lua.org/pil/9.1.html

You need to instantiate a coroutine:

co = coroutine.create(function () print("test 1"); coroutine.yield(); print("test 2"); end); coroutine.resume(co);

Note, I didn’t test the code above, so you may need to fiddle with it.

As @ apucciani says, it needs to be inside a coroutine. By that point in the article I was trying not to re-include everything in each snippet, but you should use something like the CoroPerformWithDelay () function defined earlier in the article.

I was trying what you suggested and read the article. Using the Create/Resume and all other but nothing works…

Can you provide the exact code change that need to take place?

Tnx

 Yuval

Hi.

I did something like it (a few weeks before I wrote the article) on the Corona Geek show, and it looks like I left it in that state, if you want to try that: coroutines sample

That said, this is what the full code would look like:

function CoroPerformWithDelay( delay, func, n ) local wrapped = coroutine.wrap( function( event ) func( event ) -- Do the action... return "cancel" -- ...then tell the timer to die when we're done. end ) local event2 -- Our "shadow" event. return timer.performWithDelay( delay, function( event ) event2 = event2 or { source = event.source } -- On first run, save source... event2.count = event.count -- ...update these every time. event2.time = event.time local result = wrapped( event2 ) -- Update the coroutine. It will pick up the event on the first run. if result == "cancel" then timer.cancel( event2.source ) -- After func completes, or on a cancel request, kill the timer. end end, n or 0 ) end function WaitUntilPropertyTrue( object, name, update ) while not object[name] do if update then update() end coroutine.yield() end end function DoSomethingElse () print("DONE!") end CoroPerformWithDelay(20, function() local rect = display.newRect( 100, 100, 50, 50 ) rect.isVisible = false timer.performWithDelay( 5000, function() rect.isVisible = true end ) WaitUntilPropertyTrue( rect, "isVisible" ) DoSomethingElse() -- This code should be excute 5 sec after the above line code end)