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
P#65801 2019-07-13 03:08

No - you need to setup coroutine.

P#65810 2019-07-13 07:19

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

P#65830 2019-07-14 03:46

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 ?

P#68003 2019-09-22 17:13 ( Edited 2019-09-22 22:03)

@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. :)

P#68016 2019-09-23 04:59

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 13:15:36 | 0.014s | Q:17