lua-users home
lua-l archive

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


Hello!

I'm a new lua user and I've just joined the list.

I'm using lua 5.0alpha as the scripting core for a
game engine, after also evaluating Small and lua4.
Small is smaller than lua and also seems faster, but
lua won for:
* being an overall nicer language to write in
* userdata -- great
* a more game-oriented user community
* not requiring function declaration (though already I'm
  starting to suspect that this is not an advantage...)

I chose lua 5 over lua 4 mostly because of the lightweight
userdata, and secondarily because coroutines sound so seductive.

I've been through the official documentation, the Wiki and
a few months of mailing lists.  Nonetheless I have some
comments and questions I was wondering if someone could help
with.  :)

At the moment I register a subset of the game's API
with every context that I lua_open(), then run a script
with that context.

My understanding is that lua_close() will automatically happen
when the script finishes running.  In that case am I forbidden
from running it explicitly after script completion?  As I see it,
lua_open() creates a context using dynamically-allocated
memory and lua_close() frees that memory again, so any
pointer which the application keeps to its lua_State
after the script has run is now pointing at free memory.
The manual is misleading on that matter, implying (to me)
that you can lua_close() a state if you like but you
generally do not need to, when in truth you must NOT lua_close()
a lua_State after running a script to completion with that
context.

That brings me to my real questions.  I have various
precompiled chunks in memory.  I do my script-running with
lua_pcall(L, 0, LUA_MULTRET, 0).  So, every time I run
a script its context gets implicitly destroyed upon
completion; this means that for every script invokation I
have to re-allocate a context and re-register my game's C API
functions with it.

Is there any way to re-use a context?  I think (have not
tried, yet) that I could simply do the equivilent of a
while(1){body of script; coroutine.yield();} in my LUA
scripts and the script would thus never end (with the
implicit destruction) until I want it to, and I still
get control back to the calling C code after every
run without resorting to the debugging API.  But that
seems a bit hacky.

Can an object created by a script survive beyond that
script's own lifetime?  I am confused by the manual's
concept of a 'global environment'.  The manual says
that lua uses no global variables and the whole state of
the interpreter is in the lua_State.  Fine.  But in
this case how can it be useful that this state is
implicitly destroyed upon executing a chunk?  I must
simply be missing the obvious mechanism to run multiple
chunks on a single existing lua_State; as it stands, I
can't perform multiple identical lua_pcall()s in a row.
Please help!  I must be so stupid.  :)

My final questions are hopefully just asking for 
clarification to the lua5 manual, really.  The manual
says 'If you have a C library that offers multi-threading,
then Lua can cooperate with it to implement the equivalent
facility in Lua.  Also, Lua implements its own coroutine
system on top of threads'.

Well, this surprises me a bit because my understanding of
coroutines is something like 'threadless threads', just
a really convenient way of managing state-machines with
the dirtiness hidden away, but essentially still very much
single-threaded in reality.

This would be academic except that experience has made me
afraid of system-thread-using libraries.  I'm curious
as to whether lua's coroutines really require threads as
the manual implies; it looks like my lua500 build is not
linked to any thread libraries but the tests which use
coroutine.yield() still work fine.

Thanks a lot!

--Adam
-- 
Adam D. Moss   . ,,^^   adam@gimp.org   http://www.foxbox.org/   co:3
'The difference between BSD and GPL is similar to the difference
 between sex and rape.' -- anon