Lua status()
Published Nov 9, 2023
Contribute to Docs
The status() method takes a coroutine as an argument and returns a string indicating the status of that coroutine. This status can be one of three states: suspended, running, or dead.
When a coroutine is created, it has a status of suspended by default. Once a coroutine has been called or resumed, it has a status of running. The coroutine will run until it is suspended, using coroutine.yield(), or it completes. If a coroutine completes its execution and terminates, it receives a permanent status of dead.
Syntax
The syntax for calling status() is given below:
coroutine.status(xCoroutine)
Where xCoroutine names the coroutine to be checked.
Example
The example below demonstrates the life-cycle of a coroutine from suspended to running to dead:
--Create a new coroutine using the function lifecycle()lifecycle = function(parameterCoroutine)print(coroutine.status(parameterCoroutine))endexampleCoroutine = coroutine.create(lifecycle)print(coroutine.status(exampleCoroutine)) --Prints 'suspended', since we have not yet run exampleCoroutine.coroutine.resume(exampleCoroutine, exampleCoroutine) --Runs exampleCoroutine and passes it as a parameter to lifecycle(). This prints 'running', since .status() is called on exampleCoroutine while it is executing.print(coroutine.status(exampleCoroutine)) --Prints 'dead', since exampleCoroutine has terminated.
This results in the output:
suspendedrunningdead
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
- Beginner Friendly.4 hours