Is it possible to re-enter a script with a prior context so that the lua_State of
one function can be used as the lua_State of another?
Yes; read up on coroutines. For example, this is what we do in our
engine right now:
function Ob:onTick(dt)
local fn = rawget(self, 'current_statefunc')
if fn then
local bSuccess, result = coroutine.resume(fn, self, dt)
if not bSuccess then
Tracef("Error running state '%s':\n%s", self.current_state, result)
self:setState(nil)
else
if coroutine.status(fn) == "dead" then
self.current_statefunc = nil
end
end
end
end
onTick gets called every frame/update/whatever. Often, anyway. An
object can call self:setState() on itself, which among other things,
sets up a particular function to be resumed every tick. The function
runs to completion or until it yields. The net effect is a system
where every object can pretend it has its own thread of execution.
Almost all of our object/script system is implemented in lua at the
moment (a switch from Psychonauts, which did stack manipulation and a
bunch of other stuff). This is very nice, as it allows you to play
with various semantics and implementation strategies. When you have
something you like, you can "freeze" it into C/C++ if you like, and if
it's worth it.
p