View previous topic :: View next topic |
Author |
Message |
kiddietron Site Admin
Joined: 06 Feb 2008 Posts: 797 Location: On Roblox
|
Posted: Sun May 11, 2008 1:29 am Post subject: Coroutine Tutorial by xLegox |
|
|
Have you ever wanted to make your script do soemthing like run to separate for loops at the same time, or wait for a while in on function, why still doing things? Well here's how!
I will explain here how to use the coroutine.create() and coroutine.resume()
First off, coroutine.create, creates a "thread" object. This "thread" prerforms a talsk such as the script that called it does, but it starts this taks, without stopping the current operations of the thread, it make s the 'thread''s actions happen simotaniously to the current ones in the script... here's and example.
local a = true
function run()
while a == true do
print("Going...")
wait(1)
end
end
local myCoroutine = coroutine.create(run)
coroutine.resume(myCoroutine)
wait(20)
a = false
print("Gone!")
What does this do?
first, the function "run" continualy prints "going..." untill a is false
now, loom at the line "myCoroutine = coroutine.create(run)"
this creates an object, of the type "thread", which we taged "myCoroutine"
it has the function "run" as it's body
next we have the "coroutine.resume(myCoroutine)" line
this tells the script to start playing the body of the thread, myCoroutine
next the script starts waiting, and the function "run" will continualy run while the script waits
then the script makes the variable "a" false to stop the coroutine.
and then prints "gone!"
in effect it should print...
Going...
Going...
[...]
Going...
Gone!
Hope that helps some more advanced scripters out with problems running multiple things at once =P _________________ �Kiddietron
�Phailing at making a signature
Free Money here just sign up and click away.
http://bux.to/?r=kiddietron
Spam is for noobs.
|
|
Back to top |
|
|
jtm110972 Beginner Poster
Joined: 06 Feb 2008 Posts: 33 Location: Sitting infront of a laptop
|
Posted: Sun May 11, 2008 1:32 am Post subject: |
|
|
Ok, cool. To bad I'm not that advanced... XD Though, I just thought of using multiple scripts with the Wait() function timed.. _________________ ->jtm110972<-
<a><img></a>
|
|
Back to top |
|
|
ibly3331 Beginner Poster
Joined: 31 May 2008 Posts: 17 Location: Sleeping.
|
Posted: Sat May 31, 2008 8:31 pm Post subject: |
|
|
I don't really like the coroutines, I'd rather just call the functions, what situation would make you NEED to call a coroutine? _________________ ==Violence is not the answer. It's the question. And the answer is...YES!== |
|
Back to top |
|
|
kiddietron Site Admin
Joined: 06 Feb 2008 Posts: 797 Location: On Roblox
|
Posted: Sun Jun 01, 2008 5:29 pm Post subject: |
|
|
Connections. If you didn't know, things like
Code: | function touch(p)
...
end
script.Parent.Touched:connect(touch)
|
The connection line script.Parent.Touched:connect(touch) creates a coroutine that runs the function touched. It's just a built-in connection. You'd be surprised at what coroutines can do. Especially to cut down several scripts into one. _________________ �Kiddietron
�Phailing at making a signature
Free Money here just sign up and click away.
http://bux.to/?r=kiddietron
Spam is for noobs.
|
|
Back to top |
|
|
LuigiFan Beginner Poster
Joined: 26 May 2008 Posts: 11
|
Posted: Wed Jun 18, 2008 7:32 pm Post subject: |
|
|
ibly3331 wrote: | I don't really like the coroutines, I'd rather just call the functions, what situation would make you NEED to call a coroutine? |
I mainly use it when I'm making a script using messages. With coroutines I can do other stuff while the message is waiting for removal _________________
http://www.roblox.com/User.aspx?id=31132 |
|
Back to top |
|
|
Kashi Newbie
Joined: 19 Jun 2008 Posts: 2
|
Posted: Thu Jun 19, 2008 11:22 pm Post subject: Uses for coroutine |
|
|
I was thinking of using coroutine in a minigame arena. You could have the coroutine monitoring who is still playing vs. who left while running the actual game with the rest of the script. _________________ boom. |
|
Back to top |
|
|
kiddietron Site Admin
Joined: 06 Feb 2008 Posts: 797 Location: On Roblox
|
Posted: Fri Jun 20, 2008 12:14 pm Post subject: |
|
|
Yes, ever heard of a try-catch sequence? It's often used in C++ work. Coroutines are handy for catching errors and fixing them. A minigame loop combined with coroutines is very handy. _________________ �Kiddietron
�Phailing at making a signature
Free Money here just sign up and click away.
http://bux.to/?r=kiddietron
Spam is for noobs.
|
|
Back to top |
|
|
|