Coroutine API

Functioncoroutine.create
Creates a thread from a function.
Syntax coroutine.create(
  • Function : function
)
Returns thread
Part of Lua
API Base globals
Examplecreates a thread from a function.
Create a thread from the function "print".
Code
<nowiki>
local co = coroutine.create(print)
    </nowiki>
Functioncoroutine.resume
Resumes a coroutine.
Syntax coroutine.resume(
  • coroutine : any The coroutine to resume
  • ... : any Arguments to pass to the coroutine
)
Returns boolean
Part of Lua
API Base globals
ExampleResume coroutine.
Resumes a coroutine with "test".
Code
<nowiki>
local co = coroutine.create(function(x)
  print(x)
end)
coroutine.resume(co, "test")
    </nowiki>
Output
test
Functioncoroutine.running
Returns the currently running coroutine, or nil if running outside a coroutine.
Syntax coroutine.running()
Returns thread
Part of Lua
API coroutine
ExampleGet the status of the coroutine the program is running in.
Get the status of the coroutine the program is running in.
Code
<nowiki>
coroutine.status(coroutine.running())
</nowiki>
Output running

Coroutine.status Coroutine.wrap

How To Use Coroutines

Functioncoroutine.yield
Takes arguments {...} and makes the coroutine.resume call executing the coroutine calling this to return them (usually, this is just a single string acting as a filter for the type of event desired). Usually you should use os.pullEvent and os.pullEventRaw instead.
Syntax coroutine.yield(
    {...}
)
Returns {...}
Part of CC:Tweaked
API coroutine
See also os.pullEvent
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.