do --create coroutine table --coroutine state: suspended, running, dead, normal --when create the coroutine, the status is suspended, After calling it, the status is dead --get the coroutine status by the way local coA = 0; local coB = 0; function createCoroutineA() coA = ( function() --for i = 0, 10 do print("coA: ", 0); print("coB status: ", (coB)); --normal status print("coA status: ", (coA)); print("coA coroutine next status"); --();--the current coroutine is suspended --end end ); print("From coA to resume coB"); end function createCoroutineB() coB = ( function() --for i = 0, 10 do print("coB: ", 0); print("coA status: ", (coA)); (coA); --when resume coA, the coB will suspended, calling coB ,the coA status is --suspended and dead, this time will continue to execute the next code print("coB status: ", (coB)); print("coB coroutine next status"); --(); --end end ); print("From coB to resume coA"); end --display the coA and coB status createCoroutineA(); print((coA)); createCoroutineB(); print((coB)); (coB); print((coB)); --if the coroutine is dead ,the resume will resume false, and can't resume the dead coroutine --print("coA status: ", (coA)); --print("coB status: ", (coB)); end
Note:
resume gets the return value,
If there is a corresponding yield in wait resume, then the yield parameter is used as the return value of the resume. The first return value indicates that there is no error in the coroutine. The number of subsequent return values and their values depend on the yeild parameter.
If there is no yield in wait, then the return value is the corresponding function's return value,:true,* * *
do --create coroutine table --coroutine state: suspended, running, dead, normal --when create the coroutine, the status is suspended, After calling it, the status is dead --get the coroutine status by the way local coA = 0; local coB = 0; function createCoroutineA() coA = ( function(paramA, paramB) --for i = 0, 10 do print("coA: ", 0); (paramA, paramB);--the current coroutine is suspended --end return 100, 200; end ); print("From coA to resume coB"); end function createCoroutineB() coB = ( function() --for i = 0, 10 do print("coB: ", 0); print("coA status: ", (coA)); (coA); --when resume coA, the coB will suspended, calling coB ,the coA status is --suspended and dead, this time will continue to execute the next code print("coB status: ", (coB)); print("coB coroutine next status"); --(); --end end ); print("From coB to resume coA"); end createCoroutineA(); --if not yield is waiting ,the return values that the main function return as the results of the resume --or the return as the yield params print( (coA, 10, 20));--OutPut:true, 10, 20 end