Log In  

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
P#53201 2018-06-02 04:48 ( Edited 2018-06-02 15:26)

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
P#53211 2018-06-02 09:52 ( Edited 2018-06-02 13:52)

Status check beforehand? facepalm

Thanks

P#53213 2018-06-02 11:26 ( Edited 2018-06-02 15:26)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-04-09 23:57 | 0.010s | 4194k | Q:16