lua-users home
lua-l archive

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


I lost one post here. Here it is again. This is just to illustrate an idea of coroutine messages. My script changes below are marked with --@@@.
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
You need to implement a state machine. Coroutines can be very helpful at this but they are not a showstopper in my view.

2) the current implementation of coroutines does not enable this kind of
mechanism
Coroutine messages are more powerful in my view. After all this feature is absent in C/C++ too and just about any other language out there. You can't restart you main function in the a C program, can you? I don't count long jumps as the means for this.

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

If your want to use Lua in your project I would advise you to gain certain amount of familiarity with its code base. 

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 ( )
		        if(coroutine.yield() == 'reset') return 'reset' end    --@@@                                  
        print "sequence 1"
	        if(coroutine.yield() == 'reset') return 'reset' end     --@@@                                 
        print "sequence 2"	        
        if(coroutine.yield() == 'reset') return 'reset' end      --@@@                                
        print "sequence 3"
                        if(coroutine.yield() == 'reset') return 'reset' end       --@@@                               
        print "sequence 4"
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') --@@@ this 'reset' gets propagated to the coroutine as the value returned by its most recent yield.
        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.