Trouble with Coroutines

Can’t get this to work….hummm

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

 

Here is my simple code
----------- start of code ------------

– main.lua –

local done = false

 

local function DoSomething()
     print(“Did Something”)
end

 

local function DoSomethingElse()
     print (“Did Something Else”)
end

 

local function WaitUntilTrue( func, update )
     while not func() do
          if update then
               update()
          end

          print(“yielding”)
          coroutine.yield() – attempt to yield across metamethod/C-call boundary
     end
end

 

local pos = { x = 20 }

 

DoSomething()

 

transition.to( pos, {
     x = 50,

     onComplete = function()
               done = true – NOW we’re done.
          end
     })

 

WaitUntilTrue( function()
          return done – Are we done yet?
     end )

DoSomethingElse()

 

------------ end of code ------------

 

This errors at coroutine.yield() with “attempt to yield across metamethod/C-call boundary”

am i suppose to put a coroutine.create or wrap somewhere??

 

help, i must be stupid. 

yes, you need to use create or wrap otherwise you’re not operating on a co-coroutine.

As simply as possible where do i put it???

someone please help…

Hi!

Put everything from the first call to DoSomething() up to the end inside a coroutine (you can put the rest, too, but it’s not necessary).

Without referring back to what interface I gave, something like:

-- Everything up to here is the same CoroPerformWithDelay(function() DoSomething() transition.to( pos, { x = 50, onComplete = function() done = true -- NOW we’re done. end }) WaitUntilTrue( function() return done -- Are we done yet? end ) DoSomethingElse() end)

In a revision I had comments above those that said something to the effect of “snippets” but they were too late to make it to the blog.

Thank You,

Great, i’ll give it a try and post my code when finished.

Thanks StarCrunch,

Here is my completed working code.

But it is not the result i am looking for.  I will create a new topic to address it. 

local done = false local square = display.newRect( 0, 0, 100, 100 ) local w,h = display.contentWidth, display.contentHeight local 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 local listener = function( obj ) print("Transition completed") done = true end local function DoSomething() print("Did Something") end local function DoSomethingElse() print ("Did Something Else") end local function WaitUntilTrue( func, update ) while not func() do if update then update() end coroutine.yield() end end CoroPerformWithDelay(10, function() DoSomething() transition.to( square, { time=1500, delay=2500, alpha=0, x=(w-50), y=(h-50), onComplete=listener } ) WaitUntilTrue( function() return done end ) -- Are we done yet? DoSomethingElse() end)

yes, you need to use create or wrap otherwise you’re not operating on a co-coroutine.

As simply as possible where do i put it???

someone please help…

Hi!

Put everything from the first call to DoSomething() up to the end inside a coroutine (you can put the rest, too, but it’s not necessary).

Without referring back to what interface I gave, something like:

-- Everything up to here is the same CoroPerformWithDelay(function() DoSomething() transition.to( pos, { x = 50, onComplete = function() done = true -- NOW we’re done. end }) WaitUntilTrue( function() return done -- Are we done yet? end ) DoSomethingElse() end)

In a revision I had comments above those that said something to the effect of “snippets” but they were too late to make it to the blog.

Thank You,

Great, i’ll give it a try and post my code when finished.

Thanks StarCrunch,

Here is my completed working code.

But it is not the result i am looking for.  I will create a new topic to address it. 

local done = false local square = display.newRect( 0, 0, 100, 100 ) local w,h = display.contentWidth, display.contentHeight local 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 local listener = function( obj ) print("Transition completed") done = true end local function DoSomething() print("Did Something") end local function DoSomethingElse() print ("Did Something Else") end local function WaitUntilTrue( func, update ) while not func() do if update then update() end coroutine.yield() end end CoroPerformWithDelay(10, function() DoSomething() transition.to( square, { time=1500, delay=2500, alpha=0, x=(w-50), y=(h-50), onComplete=listener } ) WaitUntilTrue( function() return done end ) -- Are we done yet? DoSomethingElse() end)