|
| 06 Apr 2014 02:29 PM |
Creative title, eh? No? ahaha. Anyways, I have a problem with coroutines. I was wondering how I could start a coroutine from the beginning instead of where it left off, without creating a new coroutine. |
|
|
| Report Abuse |
|
|
velibor
|
  |
| Joined: 24 Nov 2009 |
| Total Posts: 1003 |
|
|
| 06 Apr 2014 02:43 PM |
Use coroutine.wrap() instead of coroutine.create(), that returns a function where you can restart the coroutine with. And why don't you wish to recreate it again ?
Code coroutine.create(coroutine.resume(function() print('Hello world') end))
|
|
|
| Report Abuse |
|
|
|
| 06 Apr 2014 02:45 PM |
| I do not wish to create it again because I am going to be stopping the current coroutine when an event occurs, and I cannot create variable names with other variables so I would not know what coroutine to call. Thanks for the help, though! I'll try coroutine.wrap(). |
|
|
| Report Abuse |
|
|
velibor
|
  |
| Joined: 24 Nov 2009 |
| Total Posts: 1003 |
|
|
| 06 Apr 2014 02:51 PM |
According to the luapil the next will happen : 'If a Lua thread (Coroutine) has been yielded and has been completly done, coroutine.resume() will restart the thread.'
|
|
|
| Report Abuse |
|
|
|
| 06 Apr 2014 02:52 PM |
| Hold on, in your example you said coroutine.create, but in your statement you said to use coroutine.wrap. Which one is it? |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2014 02:53 PM |
| And, I want to restart it from the start even when it's still running the code inside of the coroutine. |
|
|
| Report Abuse |
|
|
velibor
|
  |
| Joined: 24 Nov 2009 |
| Total Posts: 1003 |
|
|
| 06 Apr 2014 02:56 PM |
coroutine.create() will create a entire new Thread. This can't be called as a Function ! coroutine.wrap() will create a Thread that can be called as a function.
-- Example Coroutine.wrap() function HelloWorld() print('Hello') end
local Func = coroutine.wrap(HelloWorld)
Func() Func()
-- Yield then, and restart.
local Func
function Hello() print('Hello world') end
function HelloWorld() Hello() coroutine.yield(Func) end
Func = coroutine.wrap(HelloWorld)
|
|
|
| Report Abuse |
|
|