[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Turning an entire script into a coroutine
- From: PA <petite.abeille@...>
- Date: Sun, 13 Feb 2005 14:08:03 +0100
On Feb 13, 2005, at 13:57, Michael Abbott wrote:
This doesn't appear possible by what I can tell because the yield()
would need to know what coroutine this script is in relation to (a
script is not a coroutine right?).
I'm fairly new to Lua as well. And even more so to the weird but
wonderful world of coroutines :)
That said, this is what I'm doing presently:
- I have a generic 'Task' object which 'wraps' any given object in a
coroutine.
- The 'tasks' register themselves with a 'runloop' which enumerator
through all the coroutines to give them a chance to run.
- When a 'task' is running, it's underlying object 'run' method is
invoked with the 'task' passed as an argument.
As an example, here is the 'run' method of a 'timer' objects which
invokes another object from time to time:
this.run = function( aTask )
repeat
if ( os.clock() - clock() >= this.interval() )
then
this.target().run( aTask, this )
resetClock()
end
aTask.yield()
until this.shouldRepeat() == false
end
http://dev.alt.textdrive.com/file/lu/LUTimer.lua
http://dev.alt.textdrive.com/file/lu/LUTask.lua
http://dev.alt.textdrive.com/file/lu/LURunLoop.lua
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/