lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


See my changes to your script below. The lines I added are marked with -- *** comment. Of course, within your current prototype this looks like a hack but you could think of a design for your engine that would use this approach and yet make code look clean and light. 

AB

-----Original Message-----
From: Benoit Germain [mailto:bgermain@ubisoft.fr]
Sent: Thursday, February 06, 2003 9:19 AM
To: Multiple recipients of list
Subject: RE: does anyone else miss a coroutine.reset() feature ?

[skipped]

And I fear that
1) I need coroutines to achieve this in a simple way
Well your objects have finite number of states and all you want to do is to do transition between these states following certain rules. You don't need co-routines to do this but they are very helpful. 

2) the current implementation of coroutines does not enable this kind of
mechanism
Not in the fashion you had envisioned it. But it gives you more powerful tool (call it co-routine messages if you will) to do this and a lot more.

3) my knowledge of the core is insufficient to add the necessary C code to
reset the coroutine

So, if anyone can prove me that #1 or #2 is wrong, or give me pointers
regarding the way to implement #3, I'd be most interested :-)

here is a little script that protoypes what I have done so far:

-- START
modeA =
{
        execute = function ( )
           local mode
           while(true) do -- ***
                if(mode == 'reset')  -- ***
                then   -- ***
                     coroutine.yield();   -- ***
                     mode = nil;   -- ***
                end   -- ***
                print "modeA"
                doSequence ( )
           end -- ***
        end
}

modeB =
{
        execute = function ( )
                print "modeB"
        end
}
 
function doSequence ( )
        local mode = coroutine . yield ( ) -- ***
        if(mode == 'reset') then return mode end -- ***
        print "sequence 1"
        mode = coroutine . yield ( ) -- ***
        if(mode == 'reset') then return mode end -- ***
        print "sequence 2"
        mode = coroutine . yield ( ) -- ***
        if(mode == 'reset') then return mode end -- ***
        print "sequence 3"
        mode = coroutine . yield ( ) -- ***
        if(mode == 'reset') then return mode end -- ***
        print "sequence 4"
        return nil -- ***
end

doModeA = coroutine . wrap (
        function ( )
                while true
                do
                        modeA . execute ( )
                        coroutine . yield ( )
                end
        end
)

doModeB = coroutine . wrap (
        function ( )
                while true
                do
                        modeB . execute ( )
                        coroutine . yield ( )
                end
        end
)

prevDoMode = doModeA
main=function(_doMode)
        if _doMode ~= prevDoMode
        then
                prevDoMode = _doMode
                -- TODO: reset _doMode so that it starts from the beginning
                prevDoMode('reset') -- ***
        end
        _doMode()
end
-- END

now, in the lua console, do:

> dofile "thisscript.lua"
> main(doModeA)
modeA
> main(doModeA)
sequence 1
> main(doModeA)
sequence 2
> main(doModeA)
sequence 3
> main(doModeB)
modeB
> main(doModeB)
modeB
> main(doModeB)
modeB
> main(doModeB)
modeB
> main(doModeA)
sequence 4

The thing is, when reverting to doModeA after calling doModeB, I want
modeA.execute() to restart from the beginning, and output "modeA" instead of
"sequence 4"


Regards,


Benoit.