Posted 10 April 2015 - 11:21 PM
Hello guys, is there any way to discover what caused a coroutine to crash/die?
Thanks in advance
Creator
Thanks in advance
Creator
PIL said:Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.
coroutine.create( function()
local ok, err = pcall( func )
if not ok then
--#error handling here
end
end )
PIL said:Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.
No, but this can be solved by wrapping your function in an error handler, something like this:
function test()
pritn("hello") --# will error
end
local ok, res = coroutine.resume( coroutine.create( test ) )
print( ok .. ":" .. res )
PIL said:Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.
No, but this can be solved by wrapping your function in an error handler, something like this:coroutine.create( function() local ok, err = pcall( func ) if not ok then --#error handling here end end )
PIL said:Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.
No, but this can be solved by wrapping your function in an error handler, something like this:
That is not true - coroutine.resume() always returns a success value and a matching second value.
E.g. if you resume a coroutine it could return true, "redstone".
"redstone" in this case would be the filter caused by the coroutine.yield() in your coroutine.
If you use coroutine.yield("key") it'd return true, "key".
If now for whatever reason the coroutine fails to run - e.g. an error, the first value will be false and the matching value will be the error message.
E.g. if you run the following code:function test() pritn("hello") --# will error end local ok, res = coroutine.resume( coroutine.create( test ) ) print( ok .. ":" .. res )
This would print out the resume result and the error message caused.
"false:attempt to call nil" or sth. like that.
PIL said:Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.
No, but this can be solved by wrapping your function in an error handler, something like this:coroutine.create( function() local ok, err = pcall( func ) if not ok then --#error handling here end end )