|
| 08 Mar 2015 01:24 PM |
I tried (for probably months checking on and off the wiki generally) to grasp coroutines, but it is one of those subjects I can't really understand. Can someone tell me if there are any better sources than the documentation or script book chapter on them, or explain it themselves? This is all I can understand:
Coroutines allow you to execute multiple functions and loops at the same time.
And three functions:
coroutine.create(f) -- creates a new thread (don't know if correct) coroutine.resume(f) -- starts the thread created ^ coroutine.wrap(f) -- works like the above two combined ^
But that is about all I can see into it. And what I said probably is misguided or incorrect. |
|
|
| Report Abuse |
|
|
|
| 08 Mar 2015 01:27 PM |
just think of them as functions that run while everything else runs, but can still change the variables within the script.
Like..
coroutine = coroutine.wrap(function() print'hi' end)
coroutine()
coroutine = coroutine.create(function() print'hi' end)
coroutine.resume(coroutine)
|
|
|
| Report Abuse |
|
|
|
| 08 Mar 2015 01:31 PM |
A pretty basic explanation is that a coroutine creates a new section of code that ROBLOX will run simultaneously with all the other sections of code (i.e. other coroutines).
Flip through this, it contains some good info: http://www.lua.org/pil/9.html
~Upload code to codepad-dot-org with Lua syntax highlighting to preserve indentation and make it easier to read!~ |
|
|
| Report Abuse |
|
|
|
| 08 Mar 2015 01:32 PM |
Your understanding of coroutine.wrap is incorrect. All it does is return a coroutine that resumes each time it is called. It usually has a yield in it.
For example:
local f = coroutine.wrap(function() local i = 1 while true do print(i) i = i + 1 coroutine.yield() end end)
f() f() f() f()
> 1 > 2 > 3 > 4 |
|
|
| Report Abuse |
|
|
|
| 08 Mar 2015 01:33 PM |
> return a coroutine that resumes each time it is called.
*return a function that resumes the coroutine each time it is called. |
|
|
| Report Abuse |
|
|
|
| 08 Mar 2015 01:51 PM |
| Thank you for the explanations. |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 08 Mar 2015 02:13 PM |
| If you're using coroutines then you're still not using them right :P |
|
|
| Report Abuse |
|
|