lua-users home
lua-l archive

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


Well, I believe the original message post in this thread specifically talked about avoiding creation of new co-routines on reset. Here is a quote from that post

I want the new
mode to start at the beginning of its body (and not where is was interrupted
the laste time is was used), whereas if the mode is unchanged, I want it to
resume where it yielded last time it was called.
The latter is easy to do, this is all what coroutines are about. But the
former is not possible unless I dump the current coroutine wrapper and
create a fresh one. I would like to avoid this, because it implies the
creation of a new collectable object each time a mode changes. Since I work
on a game project where GC hit must be kept as low as possible, it would be
much better for me if there were a coroutine.reset() call, that would
restore the coroutine to a state identical to what is obtained after a
coroutine.wrap() call.

AB

-----Original Message-----
From: Björn De Meyer [mailto:bjorn.demeyer@pandora.be]
Sent: Thursday, February 06, 2003 1:28 PM
To: Multiple recipients of list
Subject: Re: does anyone else miss a coroutine.reset() feature ?

Benoit Germain wrote:
>
> > I don't think this functionality is readily available in Lua
> > out of the box.
>
> I fear that also.

Fear not! "I'll save ya!" ^_~
A simple lua wrapper will probably allow you to
do what you want to do. Here goes:

chocoro = {} -- Cho is japanese for Ultra ;)

function chocoro.create(fun)
local result
   result = {}
   result.act = coroutine.create(fun)
   result.fun = fun
return result
end

function chocoro.resume(coro, ...)
return coroutine.resume(coro.act, unpack(arg))
end

function chocoro.yield(...)
return coroutine.yield(unpack(arg))
end

function chocoro.reset(coro)
coro.act = coroutine.create(coro.fun);
end

function AAA()
local i
i = 1
while(true) do
   print("AAA Step ", i); i = i + 1; chocoro.yield();
end
end

coro1 = chocoro.create(AAA)
chocoro.resume(coro1) --AAA Step 1
chocoro.resume(coro1) --AAA Step 2
chocoro.reset(coro1)  -- Reset the coroutine.
chocoro.resume(coro1) --AAA Step 1
chocoro.resume(coro1) --AAA Step 2

Note that I'm using coroutine.xxx syntax.
How to write a chocoro.wrap(), aswel as how
to do some sanity-checking is left as an exercice
for the reader. ^_^


--
"No one knows true heroes, for they speak not of their greatness." --
Daniel Remar.
Björn De Meyer
bjorn.demeyer@pandora.be