Main Search
Delta Corp Forum Index
 
 
FAQ Members Groups Profile Private Messages

Important Notice: We regret to inform you that our free phpBB forum hosting service will be discontinued by the end of June 30, 2024. If you wish to migrate to our paid hosting service, please contact [email protected].
Coroutine Tutorial by xLegox

 
Post new topic   Reply to topic    Delta Corp Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
kiddietron
Site Admin


Joined: 06 Feb 2008
Posts: 797
Location: On Roblox

PostPosted: Sun May 11, 2008 1:29 am    Post subject: Coroutine Tutorial by xLegox Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
jtm110972
Beginner Poster


Joined: 06 Feb 2008
Posts: 33
Location: Sitting infront of a laptop

PostPosted: Sun May 11, 2008 1:32 am    Post subject: Reply with quote

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>
Razz
Back to top
View user's profile Send private message
ibly3331
Beginner Poster


Joined: 31 May 2008
Posts: 17
Location: Sleeping.

PostPosted: Sat May 31, 2008 8:31 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
kiddietron
Site Admin


Joined: 06 Feb 2008
Posts: 797
Location: On Roblox

PostPosted: Sun Jun 01, 2008 5:29 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
LuigiFan
Beginner Poster


Joined: 26 May 2008
Posts: 11

PostPosted: Wed Jun 18, 2008 7:32 pm    Post subject: Reply with quote

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 Smile
_________________


http://www.roblox.com/User.aspx?id=31132
Back to top
View user's profile Send private message Send e-mail
Kashi
Newbie


Joined: 19 Jun 2008
Posts: 2

PostPosted: Thu Jun 19, 2008 11:22 pm    Post subject: Uses for coroutine Reply with quote

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
View user's profile Send private message Send e-mail
kiddietron
Site Admin


Joined: 06 Feb 2008
Posts: 797
Location: On Roblox

PostPosted: Fri Jun 20, 2008 12:14 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Delta Corp Forum Index -> Script Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Back to Top              
Jenova Template © digital-delusion.com
Powered by phpBB © 2001, 2002 phpBB Group



For Support - http://forums.BizHat.com

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!