A Roblox Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeLatest imagesRegisterLog 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
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
naknak
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
Supernapalm
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
m27frogy
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
slayer9365
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
myrco919
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
branefreez
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
ninga95
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
CloneTrooper787
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
raboy117
Some coroutine basics... Vote_lcapSome coroutine basics... Voting_barSome coroutine basics... Vote_rcap 
Top posting users this month
No user
Latest topics
» Where to go from here.
Some coroutine basics... EmptySeptember 14th 2020, 1:20 pm by MrNicNac

» Send me an Email
Some coroutine basics... EmptySeptember 14th 2020, 1:16 pm by MrNicNac

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

» New Site Possibly
Some coroutine basics... EmptyJuly 6th 2015, 4:16 pm by m27frogy

» Ambassador!
Some coroutine basics... EmptyApril 15th 2015, 11:40 pm by naknak

» Boop - Tag
Some coroutine basics... EmptyApril 13th 2015, 9:46 pm by naknak

» Vip Class Script
Some coroutine basics... EmptyApril 13th 2015, 4:54 pm by naknak

» Who's active?!
Some coroutine basics... EmptyApril 13th 2015, 4:52 pm by naknak

» Genesis Point
Some coroutine basics... EmptyJuly 17th 2014, 7:04 pm by branefreez

» Reward System
Some coroutine basics... EmptyJuly 17th 2014, 5:41 am by m27frogy

» Script Request
Some coroutine basics... EmptyJuly 10th 2014, 11:43 am by naknak

» local scripts?
Some coroutine basics... EmptyJuly 10th 2014, 11:39 am by naknak

» Project: Reconstruction [Died]
Some coroutine basics... EmptyJuly 10th 2014, 11:36 am by naknak

» Hi. I am new here
Some coroutine basics... EmptyApril 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
Some coroutine basics... EmptyJanuary 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
Some coroutine basics... EmptyJanuary 1st 2014, 2:11 pm by m27frogy

» Gun Making! [READ DESC]
Some coroutine basics... EmptyJanuary 1st 2014, 2:10 pm by m27frogy

» Hi, I am new here!
Some coroutine basics... EmptyNovember 26th 2013, 3:33 pm by Keanu73

» Improve Coding
Some coroutine basics... EmptyOctober 26th 2013, 1:12 pm by pook03

» Simple Button
Some coroutine basics... EmptySeptember 1st 2013, 6:19 pm by branefreez


 

 Some coroutine basics...

Go down 
3 posters
AuthorMessage
Guest
Guest
avatar



Some coroutine basics... Empty
PostSubject: Some coroutine basics...   Some coroutine basics... EmptyFebruary 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
ninga95


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

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

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


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

Some coroutine basics... Empty
PostSubject: Re: Some coroutine basics...   Some coroutine basics... EmptyMarch 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
blueymaddog


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

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

nice tut!
Back to top Go down
Sponsored content





Some coroutine basics... Empty
PostSubject: Re: Some coroutine basics...   Some coroutine basics... Empty

Back to top Go down
 
Some coroutine basics...
Back to top 
Page 1 of 1
 Similar topics
-
» Explaining the Basics of a Script with Example

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