|
| 28 Apr 2014 07:03 PM |
| Please provide a SMALL understandable example of Coroutine. |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 28 Apr 2014 07:04 PM |
| ~ http://wiki.roblox.com/index.php?title=Coroutine ~ |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 29 Apr 2014 11:02 AM |
| "These features of Lua will allow multiple threads to run alongside each other" |
|
|
| Report Abuse |
|
|