Hi intermediate scripters. Today I will be talking about coroutines.
[1] What is a coroutine? This question is common, since for people who have never heard about them, coroutines sound really weird and stuff. All this talk about thread, and routines, and statuses, all sorts of confusing stuff.
Not really.
Coroutine is actually a combination of the words "Co" and "Routine". Let's take a second to define those two words, with thanks to m-w.com
"Co-" (prefix): One that is associated in an action with another.
"Routine": A sequence of computer instructions for performing a particular task
So coroutine can logically be put together to mean a sequence of instructions that is associated in an action with another. That's fancy talk for, two different things happening simultaneously. When you run a coroutine, it runs in a seperate thread.
I have a quick metaphor for threads: And that is, actual thread (or yarn or string, for a better example :/). When running a script, it's like taking little beads and putting them on your string/thread one by one. The beads represent the code that you're telling the script to run, and the string is your thread. You have one thread, and you have to put your beads on it.
Well, when you make a coroutine, you're creating a new thread, or bringing in another piece of string. The strings are only as long as they have to be in order to run the code given to them, so once you fill up the string, you can't do anything else with it.
Now, when you have more than one thread, the script will put beads on all of them. It won't finish one thread, then go through all of the next thread, etc. It goes through all threads at the same time, though I hesitate to say it does them at the same time. Let's just say it does it
basically at the same time.
So that's basically what a coroutine is and what it does, so now let's get on with the next section.
[2] How to use a coroutine So now that you've learned what coroutines are, you're thinking that you want to use one now. First, let's see all the stuff about coroutines that you can do.
coroutine.createThis function allows you to create a new coroutine. It takes a function as its argument and returns the new coroutine.
- Code:
-
c = coroutine.create(function() wait(5) print("Hi") end)
Right now, nothing would be happening. We'll explain that in a second.
coroutine.statusCoroutine.status returns a string that tells the status of the coroutine. The statuses are "suspended", "running", and "dead". When it's suspended, it is doing nothing. When it's running, obviously, it runs the stuff inside of it until it gets stopped. A coroutine becomes "dead" when it finishes running everything inside of it. There is no way to resurrcet a coroutine, so once it's run its code, it's done.
- Code:
-
print(coroutine.status(c)) --> suspended
coroutine.resumeThis function takes a suspended coroutine as its first argument, and any arguments you may want to pass to the function as the 2nd and subsequent arguments. It then starts the coroutine, changing its status from suspended to running. The function returns true as its first argument if there's no errors, and if it was yielded it returns arguments passed to the yield (explained soon), and if it ended, it returns what the function returns. So for example:
- Code:
-
pie = coroutine.create(function(a, b, c)
print(a, b, c)
return 4
end)
d, e = coroutine.resume(pie, 1, 2, 3) --> 1 2 3 is printed
print(d, e) --> true 4 is printed
If the coroutine errored, then it returns false and the error message.
- Code:
-
pie = coroutine.create(function()
pprint("Hai")
end)
print(coroutine.resume(pie)) --> false Attempt to call global 'pprint' (a nil value)
coroutine.yieldThis function stops the coroutine that it's inside. It changes the status to suspended.
- Code:
-
c = coroutine.create(function()
print("Hi")
coroutine.yield()
print("Bye")
end)
coroutine.resume(c) --> Hi
coroutine.resume(c) --> Bye
print(coroutine.status(c)) --> dead
[3] That's all, folks! For a BUNCH more information about this stuff, you can go to the Roblox wiki or to the Lua website. I don't have enough knowledge about this stuff to do more, so all I can really do is give the basics.
And before you call this advanced, let me give my own argument in return. I'd say the other stuff in this forum is too simple. How do ya like them apples?
Feedback?