Difference between revisions of "Coroutine (type)"

From ComputerCraft Wiki
Jump to: navigation, search
m (moved Coroutine to Coroutine (type): Now we can link to this from Template:Type)
(Remove old crud, simplify, and point back at the API page for more details, which now exist)
 
Line 1: Line 1:
{{NeedsWork|Why is the safeguard there? What is its purpose? ''[[User:Pokepal101|Pokepal101]] 03:35, 1 January 2013 (MSK)''}}
+
''This is for the coroutine type, displayed as <code>thread</code> in the Lua language. For the API, visit [[Coroutine (API)]].
  
''This is for the coroutine object. For the API, visit [[Coroutine (API)]].
+
A ''coroutine'', or ''thread'', is an object which contains an execution context in a Lua program. A coroutine wraps up the location of the next statement to execute, the complete call stack of functions called to reach that statement, the local variables in those functions, and any other data needed to freeze, and subsequently thaw and resume, a running code sequence, as though nothing happened.
  
Coroutines are objects that allow you to form multiple tasks at the same time.
+
For details on how coroutines are used, see the [[Coroutine (API)|coroutine API]].
  
== How to Make a Coroutine ==
+
[[Category:Lua Types]]
 
+
Type 'lua' in the terminal.
+
 
+
To create a variable, you use the function [[coroutine.create|coroutine.create()]]. Inside the parentheses, you need to add a function.
+
First, make a function:
+
 
+
lua> function helloWorld() for i = 1,10 do sleep(1) print(i) end end
+
 
+
This will create a function named ''helloWorld'' that will print a consecutive number every second for 10 seconds.
+
Now that you have a function, create the coroutine using the function:
+
 
+
lua> local c = coroutine.create(helloWorld)
+
 
+
Note that you don't use the parentheses in the command. This is how a function looks like in variable form.
+
Now, let's run it:
+
 
+
lua> coroutine.resume(c)
+
 
+
First, it gives 1, but then returns... "timer"? What's that all about?
+
 
+
The "timer" you see there is a safeguard in case something harmful happens. However, assuming that the safeguard wasn't there, you would be able to type both commands and see the text appear there!
+
 
+
== Why Use Coroutines? ==
+
 
+
Coroutines are used to do multiple things at the same time.
+
 
+
However, only use coroutines when you believe you have no other choice, because many issues can occur with coroutines.
+
 
+
[[Category:Tutorials]]
+

Latest revision as of 18:24, 22 April 2013

This is for the coroutine type, displayed as thread in the Lua language. For the API, visit Coroutine (API).

A coroutine, or thread, is an object which contains an execution context in a Lua program. A coroutine wraps up the location of the next statement to execute, the complete call stack of functions called to reach that statement, the local variables in those functions, and any other data needed to freeze, and subsequently thaw and resume, a running code sequence, as though nothing happened.

For details on how coroutines are used, see the coroutine API.