A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeFAQRegisterLog in
If you're a experienced coder make some tutorials! It would really help the site grow.
Make sure you read the rules(Which can be found by clicking here)
If you're a beginner at coding, try some tutorials.
We have many Moderators/Admins watching this site. Contact them with Questions.
Let us know what your favorite sport is. By clicking here to vote (Click here)
This site is becoming inactive. Lets make it active.
Log in
Username:
Password:
Log in automatically: 
:: I forgot my password
Top posters
blueymaddog
 
naknak
 
Supernapalm
 
m27frogy
 
slayer9365
 
myrco919
 
branefreez
 
ninga95
 
CloneTrooper787
 
raboy117
 
Top posting users this month
Latest topics
» Send me an Email
January 8th 2016, 10:14 am by naknak

» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]
July 6th 2015, 7:38 pm by m27frogy

» New Site Possibly
July 6th 2015, 4:16 pm by m27frogy

» Ambassador!
April 15th 2015, 11:40 pm by naknak

» Boop - Tag
April 13th 2015, 9:46 pm by naknak

» Vip Class Script
April 13th 2015, 4:54 pm by naknak

» Who's active?!
April 13th 2015, 4:52 pm by naknak

» Where to go from here.
April 13th 2015, 4:51 pm by naknak

» Genesis Point
July 17th 2014, 7:04 pm by branefreez

» Reward System
July 17th 2014, 5:41 am by m27frogy

» Script Request
July 10th 2014, 11:43 am by naknak

» local scripts?
July 10th 2014, 11:39 am by naknak

» Project: Reconstruction [Died]
July 10th 2014, 11:36 am by naknak

» Hi. I am new here
April 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
January 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
January 1st 2014, 2:11 pm by m27frogy

» Gun Making! [READ DESC]
January 1st 2014, 2:10 pm by m27frogy

» Hi, I am new here!
November 26th 2013, 3:33 pm by Keanu73

» Improve Coding
October 26th 2013, 1:12 pm by pook03

» Simple Button
September 1st 2013, 6:19 pm by branefreez


Share | 
 

 Some coroutine basics...

Go down 
AuthorMessage
Guest
Guest



PostSubject: Some coroutine basics...   February 8th 2011, 4:27 pm

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.create

This 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.status

Coroutine.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.resume

This 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.yield

This 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?
Back to top Go down
ninga95
Administrator
Administrator
avatar

Posts : 122
Join date : 2010-07-30
Age : 22

PostSubject: Re: Some coroutine basics...   February 9th 2011, 9:50 pm

nice
Back to top Go down
lukenuke75
Novice Scripter
Novice Scripter
avatar

Posts : 38
Join date : 2011-03-07
Age : 21

PostSubject: Re: Some coroutine basics...   March 7th 2011, 3:33 pm

Didnt understand one lick of it XD

Well i did but i need to learn how to use functions like you did in the examples.



Great Job!
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 19

PostSubject: Re: Some coroutine basics...   March 27th 2011, 6:24 am

nice tut!

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Sponsored content




PostSubject: Re: Some coroutine basics...   

Back to top Go down
 
Some coroutine basics...
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
A Roblox Community :: Tutorials and Resources :: Intermediate-
Jump to: