|
| 18 Aug 2014 05:44 PM |
In a previous thread several people pointed out that I can use coroutine to fire a function independently from another function. However, I needed to that independently fired function more than once, and it seems that coroutine.create and coroutine.resume only allow that function to be used once. I'm not trying to use event handlers to start functions. Example:
local doSomething = coroutine.wrap(function(num) wait(10) print("doSomething Test" .. num) end)
function main() print("main Test1") for i = 1, 5 do doSomething(i) end print("main Test2") end
main()
It will only fire doSomething once before giving me an error about it being a dead function or not a function at all. It works one time before this, and I have to reset PlaySolo in order to restart the script.
I'm trying to output:
main Test1 main Test2 doSomething Test1 -- Each of these should appear instantly around 10 seconds doSomething Test2 -- later doSomething Test3 doSomething Test4 doSomething Test5
Instead of:
mainTest1 mainTest2 Example.Example:8: cannot resume dead coroutine |
|
|
| Report Abuse |
|
|
L0cky2013
|
  |
| Joined: 30 Jul 2012 |
| Total Posts: 1446 |
|
|
| 18 Aug 2014 05:50 PM |
Me, I would use spawn in this case.
function doSomething(number) Spawn(function() wait(10) print("doSomething Test" .. number) end) end |
|
|
| Report Abuse |
|
|
|
| 18 Aug 2014 06:26 PM |
I am using Spawn but am not getting the results I need. I need to run a function without it interfering with the current function's timing.
local bleh = (function() wait(5) print("bleh Test1") end
function main() print("main Test1") Spawn(Bleh()) print("main Test2") end
main()
Would give me an output of:
main Test1 bleh Test1 -- After waiting for 5 seconds main Test2
because main() runs the code from bleh() before continuing. I don't want that to happen. |
|
|
| Report Abuse |
|
|
|
| 18 Aug 2014 06:47 PM |
Spawn(Bleh())
will call Bleh and pass what it returns to the Spawn function. You do not want this. Fixed code:
local bleh = function() wait(5) print("bleh Test1") end
function main() print("main Test1") Spawn(bleh) print("main Test2") end
main() |
|
|
| Report Abuse |
|
|
|
| 18 Aug 2014 08:49 PM |
| Thank you very much, this has answered my question. Up until this point I didn't even know that coroutine and Spawn were things, reflects on my knowledge of Lua :/ |
|
|
| Report Abuse |
|
|