generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: What does Coroutine do?

Previous Thread :: Next Thread 
WickedSiege is not online. WickedSiege
Joined: 03 Oct 2011
Total Posts: 3039
28 Apr 2014 07:03 PM
Please provide a SMALL understandable example of Coroutine.
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
28 Apr 2014 07:04 PM
Its like a function that you can stop and resume within loops or other functions and even other coroutines.
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
28 Apr 2014 07:04 PM
~ http://wiki.roblox.com/index.php?title=Coroutine ~
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
28 Apr 2014 07:05 PM
Purpose; Multiple loops/function/events at one time(Things with wait methods that you want to happen spontaneously)
Report Abuse
Prescience is not online. Prescience
Joined: 26 Apr 2014
Total Posts: 188
28 Apr 2014 07:06 PM
a coroutine is meant to run a separate thread (or contextual 'script') inside your already running thread (which is the actual script your working in).

coroutine.create(function()end) is for declaring yourself functionality inside a coroutine,

coroutine.resume(cc,arguments) (where cc is a coroutine.create instanciation, and arguments is whatever else you want to pass to the function inside the coroutine)

and coroutine.wrap(function() --[[code]] end)()

coroutine.wrap is a free standing 1-call coroutine that runs in that place in the script separate from your current script.

the purpose of coroutines is to make it so you could have essentially multiple 'scripts' running in the same script at the same time without interfering with each other.
Report Abuse
ReCoVeRyFlAsH is not online. ReCoVeRyFlAsH
Joined: 21 Nov 2010
Total Posts: 107
28 Apr 2014 07:07 PM
ok so basically you can multitask, you can run a function that has a wait in it and still run other code.


Report Abuse
Prescience is not online. Prescience
Joined: 26 Apr 2014
Total Posts: 188
28 Apr 2014 07:07 PM
anything outside the scope of the coroutine is also usable to the coroutine. It works the same way as any code block as far as what they can do with variables that are local or not.
Report Abuse
wazap is not online. wazap
Joined: 29 Jun 2007
Total Posts: 23234
28 Apr 2014 07:08 PM
ok

local a = 0
local b = 0

while true do
a = a+1
wait(.1)
print(a+b)
end

while true do
b = b+1
wait(.1)
end

The above code will print 1, 2, 3, 4, 5, 6, 7, 8... and so forth, as the second loop is never run.

local a = 0
local b = 0

coroutine.resume(coroutine.create(function()
while true do
a = a+1
wait(.1)
print(a+b)
end
end))

while true do
b = b+1
wait(.1)
end

the above coroutine makes it so that both loops are run at the same time.
And as a result will print
1, 3, 5, 7, 9, 11... and so forth, I believe. Might be 2, 4, 6, 8,...
Report Abuse
WickedSiege is not online. WickedSiege
Joined: 03 Oct 2011
Total Posts: 3039
29 Apr 2014 10:40 AM
Ok, so would this work?

coroutine.create(function(r))
print'blah'
end) --Im not typing in studio, so I don't know.


wait(2)
coroutine.resume(r)


This doesn't look right xD
Report Abuse
masterCameron101 is not online. masterCameron101
Joined: 28 Mar 2010
Total Posts: 1377
29 Apr 2014 11:01 AM
So Coroutines are like threads but in Lua?

~masterCameron101, advanced Lua programmer, certified graphics designer and geek~
Report Abuse
ReCoVeRyFlAsH is not online. ReCoVeRyFlAsH
Joined: 21 Nov 2010
Total Posts: 107
29 Apr 2014 11:02 AM
"These features of Lua will allow multiple threads to run alongside each other"
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image