Log In  


does yield() work outside of coroutines and inside functions?

I feel coroutines are a pain to setup and update when it would just be easier to call yield inside a function
not to mention the tokens required to setup coroutines and call/update them

function wait(t)
  for x=1,t do yield() end
end

function test()
 wait(2)
 print("test")
 wait(2)
end

function _update()
 test()
end


No - you need to setup coroutine.


that's too bad i feel it just takes up tokens to do so.


Fully agree. I made a note of that recently myself. Would be nice for ZEP to add that ability. And yet, just now thinking ... if it did that, what program would be the controlling routine ?


@Shadowblitz16, what would you expect your code sample to do?

It looks like you want yield() to return control all the way up to the game loop, then on the next _update() resume control to the yield point. You can do this with a master coroutine in _update().

...

function update()
 test()
 return true
end

function _init()
 cor = nil
 status = true
end

function _update()
 if cor and status then
  status, result = coresume(cor)
  if result or not status then
   cor = nil
  end
 end
 if not cor then
  cor = cocreate(update)
  status, result = coresume(cor)
 end 
end

This could probably be more concise, I just whipped this up quickly.

Care must be taken to not drop frames when the coroutine runs out. In this version, I'm using the return value of the coroutine (update()) as a signal that the coroutine needs to be restarted. The status value alone (the first return value from coresume() and the value of costatus()) will stay true in the iteration that the coroutine returns and doesn't yield, so if you go by status alone you end up skipping a beat until the next call to coresume actually kills the coroutine.

I look forward to someone improving my answer. :)



[Please log in to post a comment]