create()
Published Oct 19, 2023
Contribute to Docs
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’.
Syntax
co = coroutine.create(newFunc)
Where newFunc
is used as an argument to run within the new coroutine.
Example
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:
suspendedHi, Alicetruedead
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours