|
| 08 Aug 2016 08:34 AM |
what are they and how can they be helpful?
much thks |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 08 Aug 2016 08:36 AM |
| They are what allow multiple scripts to seem like they are running at the same time. |
|
|
| Report Abuse |
|
|
|
| 08 Aug 2016 08:37 AM |
can you tell me how they can be useful? am too lazy to look it up |
|
|
| Report Abuse |
|
|
|
| 08 Aug 2016 08:38 AM |
coroutines allow for multiple threads to run on the same script
usually, in 1 script, only 1 thing can be done at a time and the whole script yields for every wait()
with a coroutine, you can have 2 threads running at once without having to create to scripts
for example, you could have two while loops running at the same time, whereas in a normal script without coroutines, you can't do this |
|
|
| Report Abuse |
|
|
|
| 08 Aug 2016 08:39 AM |
Spawn() and Delay() are what I use because they create new threads but they are way easier than coroutines, but coroutines are a bit more powerful.
They are useful for running functions that yield without yielding the script, running two loops at once, and other stuff |
|
|
| Report Abuse |
|
|
|
| 08 Aug 2016 08:39 AM |
oh wow
theres been so many situations where i wish they added this functionality much thanks |
|
|
| Report Abuse |
|
|
|
| 08 Aug 2016 08:43 AM |
| Spawn, Delay, coroutines, etc. do not create actual threads, it's all done via collaborative multi-threading. One coroutine (Spawn and Delay create coroutines) is ever running at once time. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
| |
|
| |
|
|
| 08 Aug 2016 08:50 AM |
But yes it gives the appearance of things happening at the same time. I remember using a Ceramic example a long time ago, something along the lines of like:
Without coroutines: - Wedge Clay 1 - Form Clay 1 - Fire Clay 1 - Wait - Glaze Clay 1 - Fire Clay 1 - Wait - Repeat for Clay 2 -> Clay 5
With coroutines: - Wedge Clay 1 - Form Clay 1 - Fire Clay 1 - Repeat for Clay 2 -> Clay 5 - Glaze Clay 1 - Fire Clay 1 - Repeat for Clay 2 -> Clay 5
So pretty much, instead of "waiting" you could be more efficient by doing other things. Of course the above example is stupid but I mean I hope it gets the point across. |
|
|
| Report Abuse |
|
|
|
| 08 Aug 2016 08:54 AM |
it sure does thanks for the help guys! |
|
|
| Report Abuse |
|
|
|
| 08 Aug 2016 09:28 AM |
coroutines are similar to spawn
usually the most useful uses are only
coroutine.wrap(function()
end)()
and
spawn(function()
end)
These both will run whatever is inside as if you ran it in a different script except coroutine.wrap() returns a function which resumes that coroutine so calling it with () will resume it.
The reason why this is one of the only useful ways is because in functions you can always just do function DoSomething = coroutine.wrap(function()
end)
to create a function that acts like a coroutine |
|
|
| Report Abuse |
|
|
| |
|
pketny
|
  |
| Joined: 27 Dec 2010 |
| Total Posts: 1162 |
|
|
| 08 Aug 2016 09:38 AM |
http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#Spawn
'Executes function f after thread next yields.' - Spawn will not run until the current script waits for something (wait() is called or WaitForChild())
Be careful when using Spawn().
coroutine will run instantly:
http://wiki.roblox.com/index.php?title=Function_dump/Coroutine_manipulation
Example why you have to be careful:
http://wiki.roblox.com/index.php?title=Thread_scheduler
|
|
|
| Report Abuse |
|
|