Beginners Guide to Coroutines: Difference between revisions
>Samacado No edit summary |
>Pwnedu46 Fixed grammatical errors, added the "See Also" section, replaced <code lua></code> with {{code and output}} formatting. |
||
Line 1: | Line 1: | ||
== What is a coroutine? == | == What is a coroutine? == | ||
Coroutines are some of the most interesting, useful parts of lua yet still some of the most misunderstood. When you make a new coroutine, it will create a new '''thread''' which is sort of like adding a new script to your place. Be | Coroutines are some of the most interesting, useful parts of lua yet still some of the most misunderstood. When you make a new coroutine, it will create a new '''thread''' which is sort of like adding a new script to your place. Be careful though, too many threads mean a lot of lag! | ||
== How to create a coroutine == | == How to create a coroutine == | ||
Line 13: | Line 13: | ||
</code> | </code> | ||
Now you've created your first coroutine and started a new thread! Yet | Now you've created your first coroutine and started a new thread! Yet nothing has happened, well this is because you haven't run it. To run your coroutine you will need to use ''coroutine.resume()'' and the parameter will be the thread you created. | ||
{{code and output|code = | |||
function core() | function core() | ||
print("hola") | print("hola") | ||
Line 23: | Line 23: | ||
coroutine.resume(new_thread) | coroutine.resume(new_thread) | ||
|output= hola}} | |||
Let's say you wanted to call parameters in your coroutine's function, well then what you will need to do that with is ''coroutine.resume()'' like so | |||
{{code and output|code = | |||
function core(a,b,c) | function core(a,b,c) | ||
print(a*b+c) | print(a*b+c) | ||
Line 35: | Line 35: | ||
coroutine.resume(new_thread,3,5,6) | coroutine.resume(new_thread,3,5,6) | ||
|output = 21}} | |||
== coroutine.wrap == | == coroutine.wrap == | ||
''coroutine.wrap()'' can be used in a way, as a replacement for ''coroutine.resume'' and ''coroutine.create''. You use ''coroutine.wrap'' on the function like ''coroutine.create'' but you will use the variable you asign it to like a function. Think of ''coroutine.wrap'' as a function with a coroutine shoved inside it. | ''coroutine.wrap()'' can be used in a way, as a replacement for ''coroutine.resume'' and ''coroutine.create''. You use ''coroutine.wrap'' on the function like ''coroutine.create'' but you will use the variable you asign it to like a function. Think of ''coroutine.wrap'' as a function with a coroutine shoved inside it. | ||
{{code and output|code = | |||
function core() | function core() | ||
print("Hola") | print("Hola") | ||
Line 50: | Line 48: | ||
new_thread() | new_thread() | ||
|output = Hola}} | |||
If you want to add in parameters, just do it like you would any other function. | |||
{{code and output|code = | |||
function core(a,b,c) | function core(a,b,c) | ||
print(a*b+c) | print(a*b+c) | ||
Line 62: | Line 60: | ||
new_thread(8,2,1) | new_thread(8,2,1) | ||
|output=17}} | |||
== What are coroutines really useful for? == | == What are coroutines really useful for? == | ||
You have learned | You have learned a couple of the coroutine functions, but what can you use them for? Well one of the most useful things is making loops and functions run at the same time. | ||
<code lua> | <code lua> | ||
Line 93: | Line 90: | ||
''coroutine.yield'' puts your coroutine in suspended mode, where it just stops and waits until the coroutine is called again. ''coroutine.yield'' cannot include a metamethod, C function or an iterator. ( if you don't know what those are then you're most likely not using them. ) Anything extra put inside a ''coroutine.yield'' will go straight to ''coroutine.resume''. | ''coroutine.yield'' puts your coroutine in suspended mode, where it just stops and waits until the coroutine is called again. ''coroutine.yield'' cannot include a metamethod, C function or an iterator. ( if you don't know what those are then you're most likely not using them. ) Anything extra put inside a ''coroutine.yield'' will go straight to ''coroutine.resume''. | ||
{{code and output|code = | |||
function core(param) | function core(param) | ||
print (param) | print (param) | ||
Line 103: | Line 100: | ||
new_thread("Hola mi amigos!") | new_thread("Hola mi amigos!") | ||
new_thread("This was in yield :O") | new_thread("This was in yield :O") | ||
|output = | |||
Hola mi amigos! | |||
yield : This was in yield :O | |||
}} | |||
=== coroutine.status === | === coroutine.status() === | ||
''coroutine.status'' will tell you if your coroutine is either dead, suspended,running or normal. It will return this as a string to you. What do those mean? | ''coroutine.status()'' will tell you if your coroutine is either dead, suspended, running or normal. It will return this as a string to you. What do those mean? | ||
*running means that the coroutine is currently working and using its code. | *running means that the coroutine is currently working and using its code. | ||
*dead means that the coroutine has stopped running and is done for now. | *dead means that the coroutine has stopped running and is done for now. | ||
Line 117: | Line 114: | ||
Now how do we use it? We simply do | Now how do we use it? We simply do | ||
{{code and output|code = | |||
function core() | function core() | ||
print("hola") | print("hola") | ||
Line 129: | Line 126: | ||
print(coroutine.status(new_thread)) | print(coroutine.status(new_thread)) | ||
|output = | |||
suspended | |||
hola | |||
suspended | dead | ||
hola | }} | ||
dead | |||
=== coroutine.running() === | === coroutine.running() === | ||
''coroutine.running()'' will return the current thread that is running. Example | ''coroutine.running()'' will return the current thread that is running. Example: | ||
{{code and output|code = | |||
new_thread=coroutine.create(function() | new_thread=coroutine.create(function() | ||
print("hola") | print("hola") | ||
Line 146: | Line 141: | ||
print(coroutine.running()) | print(coroutine.running()) | ||
| output = | |||
A hexadecimal memory location where the coroutine is stored. (i.e. thread: 183E0548) | |||
}} | |||
== See Also == | |||
* [http://wiki.roblox.com/index.php/Function_Dump/Coroutine_Manipulation Coroutines] | |||
* [http://www.lua.org/pil/9.html Programming in Lua: Coroutine Basics] |
Revision as of 06:43, 22 January 2012
What is a coroutine?
Coroutines are some of the most interesting, useful parts of lua yet still some of the most misunderstood. When you make a new coroutine, it will create a new thread which is sort of like adding a new script to your place. Be careful though, too many threads mean a lot of lag!
How to create a coroutine
Creating a coroutine is fairly simple, all you need to start is a function and coroutine.create(). The one parameter of coroutine.create will be the function you wish to run, so like this.
function core()
print("hola")
end
new_thread=coroutine.create(core)
Now you've created your first coroutine and started a new thread! Yet nothing has happened, well this is because you haven't run it. To run your coroutine you will need to use coroutine.resume() and the parameter will be the thread you created.
Let's say you wanted to call parameters in your coroutine's function, well then what you will need to do that with is coroutine.resume() like so
coroutine.wrap
coroutine.wrap() can be used in a way, as a replacement for coroutine.resume and coroutine.create. You use coroutine.wrap on the function like coroutine.create but you will use the variable you asign it to like a function. Think of coroutine.wrap as a function with a coroutine shoved inside it.
If you want to add in parameters, just do it like you would any other function.
What are coroutines really useful for?
You have learned a couple of the coroutine functions, but what can you use them for? Well one of the most useful things is making loops and functions run at the same time.
local h=Instance.new("Hint",workspace);
local m=Instance.new("Message",workspace);
coroutine.resume(coroutine.create(function()
for i=60,0,-1 do
Wait(0.5)
h.Text=i
end end -- one for the for, one for the function
)--closes create
)--closes resume
for i=60,0,-1 do
Wait(1)
m.Text=i
end
As you can now see, the message and the hint both change their text at the same time but at different speeds however, they could be the same speed yet for example purposes, they weren't.
more, more coroutines
coroutine.yield
coroutine.yield puts your coroutine in suspended mode, where it just stops and waits until the coroutine is called again. coroutine.yield cannot include a metamethod, C function or an iterator. ( if you don't know what those are then you're most likely not using them. ) Anything extra put inside a coroutine.yield will go straight to coroutine.resume.
coroutine.status()
coroutine.status() will tell you if your coroutine is either dead, suspended, running or normal. It will return this as a string to you. What do those mean?
- running means that the coroutine is currently working and using its code.
- dead means that the coroutine has stopped running and is done for now.
- suspended means coroutine.yield() ran and it's waiting to start up again.
- normal means it hasn't been told to start running yet.
Now how do we use it? We simply do
coroutine.running()
coroutine.running() will return the current thread that is running. Example: