Hi,
Coroutines are an essential part of my game engines.
However, I don't get how I can do both:
- raise an error when something bad happens
- silently handle proper coroutine termination
coresume returns a pair (is running,exception).
Thing is exception is set even during normal termination!!
note: costatus doesn't help as it immediately flips to "dead" when the coroutine crashes.
Something like that doesn't work, e.g. I get an assert even after genuine termination:
local co function _init() co=cocreate(function() for i=1,10 do yield() end -- syntax error test -- i=k/10 end) end function _update() if co then local r,e=coresume(co) print(costatus(co)) if not r then if(e) assert(r,e) -- never reached co=nil end end end |
you're doing a coresume on a dead coroutine, it returns an error as it should.
try checking its status beforehand:
if co then local cs=costatus(co) if cs=="suspended" then print("suspended") local r,e=coresume(co) if (not r) assert(r,e) elseif cs=="dead" then print("done") co=nil else print("running") end end |
[Please log in to post a comment]