Starts a coroutine, or resumes a suspended coroutine.
- The coroutine, as created by
- The arguments to pass to the coroutine's function or the coroutine's subsequent yields.
A coroutine is a special kind of function that can yield control back to the caller without completely exiting. The caller can then resume the coroutine as many times as needed until the function exits.
coresume() function starts or resumes a coroutine created by
cocreate(). Control returns to the coroutine function until the function yields or exits.
true if the given coroutine was active (suspended) when
coresume() was called, or
false if it was given a dead coroutine (and no code was executed by resuming). If a resumed coroutine function exits (rather than yields), the coroutine goes from suspended to dead, and a subsequent call to
coresume() with the same coroutine will return
false. It is more typical to use the
costatus() function to test the status of a coroutine, but the return value from
coresume() may be used instead.
In the event that
false, it may also return a second value containing a string describing an exception that caused the routine to die unexpectedly, e.g. "attempt to index a nil value". A full stack trace can also be obtained by passing the dead coroutine to
trace(). In fact, the code below is a very simple way to
stop() with full stack trace when a coroutine dies unexpectedly:
local active, exception = coresume(co) if exception then stop(trace(co, exception)) end
coresume() may also return
true if the coroutine did not finish executing in time, even if it didn't return or yield. In this case, the coroutine remains in the suspended state and can be resumed later, just as if it explicitly yielded.
coresume() does not return a value from the coroutine function, nor can
yield() accept a value. If the function exits by returning a value, this value is ignored. (Lua and other languages support yielding and returning values from coroutines, but PICO-8 does not.)
coresume() may pass extra arguments to the coroutine. These arguments will be mapped to the coroutine's function arguments on the first call, and will be used as return values for
yield() on subsequent calls.
function foo(a, b, c) print(a) print(b) print(c) a, _, c = yield() print(a) print(b) print(c) end local thread = cocreate(foo) coresume(thread, 1, 2, 3) --> prints: 1 2 3 coresume(thread, 4, 5, 6) --> prints: 4 2 6
cocreate() for a general example.