create() is used to create a new coroutine with a function as an argument that will run within the new coroutine. This function returns a type thread value. When a new coroutine is created, the status is ‘suspended’. The
.resume() method is used to run or initiate the coroutine. When the execution has completed the coroutine is designated ‘dead’.
co = coroutine.create(newFunc)
newFunc is used as an argument to run within the new coroutine.
The following example shows the implementation of the function and its output.
-- Create a new coroutine named co_greatco_great = coroutine.create(function (name)print("Hi, " .. name)end)-- Check a new coroutine statusprint(coroutine.status(co_great))-- Run a new coroutineprint(coroutine.resume(co_great, "Alice"))-- Check again after it was runprint(coroutine.status(co_great))
The output will look like this below: