[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Threads vs Coroutines and Wait()
- From: "John Haggerty" <slm@...>
- Date: Mon, 7 Mar 2005 21:44:35 -0500
Hi, I'm a new user of Lua and, as usual, I'm trying to use it as a scripting
language for my game.
I want to have a Wait() function which takes a length of time and "pauses"
for that time. Of course, during this time, gameplay must continue, so what
really needs to happen is the script has to stop executing, and be resumed
I figured I'd use coroutines to do this. Wait() could tell the game engine
to resume the current coroutine in X seconds, and then yield. Gameplay would
continue, and the game engine would later resume the coroutine.
But a problem occurs when the script writer starts using coroutines on their
own. If they create a coroutine and run it, and then *that* coroutine calls
Wait(), then it will yield back to their own original coroutine rather than
all the way back to the game engine.
I was hoping that using "Threads" as described in 3.20 in the documentation
might avoid this problem, and that lua_resume/lua_yield doesn't take Lua's
coroutine.resume() and coroutine.yield() into account. But after reading
some messages in this group I'm worried that lua_resume and lua_yield are
essentially identical to coroutine.resume() and coroutine.yield(), so my
Is there any way to achieve what I want? Is there any sort of tricky usage
of coroutines that avoids the problem, or is there a solution that doesn't
involve coroutines at all? Or will I have to require that the user not call
Wait() from within a coroutine (and can this be automatically enforced)?
Thanks for any help or explanation you can offer. Sorry if I've unknowingly
violated any unwritten community rules, as this is my first post here. =)
(As a side note, why does 3.20 talk about "Threads" while 2.10 talks about
"Coroutines?" Is there a difference or is it just a different name depending
on whether you're calling the functions from C code or from Lua?)
[ http://www.slimeland.com/ ]