lua-users home
lua-l archive

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

Every time you spawn a thread it automatically gets the global table of
the state that created it, IE every thread shares the same memory. You
need to put a new global table in the thread after you create it if you
want it to have its own memory so it can have its own version of

In order to still use the shared global memory you're going to need to
add new metamethods to the new thread global table to defer searches to
the old shared global table if the data isn't found in the local one. 


-----Original Message-----
[] On Behalf Of S. Brekelmans
Sent: Saturday, October 18, 2003 11:07 AM
Subject: Re: Running a script in each Lua thread.


I've searched through the archives and it has been very useful in
me started with lua. I'm at a bit of an impass right now and am hoping
someone on the list can help.

Currently I am opening a lua_State, registering all of my C functions
that, and then creating a new thread for each entity I need using
lua_newthread(). Subsequent to creatring a new thread, I load a script
to be run by that thread, and then use lua_getglobal(thread, "funcname")
to find the function I want to call in the script. then I call
This works perfectly as long as the function name is unique. The problem
I would like to have each script have an Initialize() function.
Unfortunately, the Initialize() lua function of whichever script was
loaded gets called no matter which lua thread I am resuming.

What is the proper way of doing this? My application, like so many
is in a game system where I would like to be able to create new game
entities, attach a lua script to then, and let the script run, using
from within C functions to yield the scripts. I also would like to keep
names identical across all scripts to simplify the calling interface
C code.
I've tried using multiple lua_open() calls to create seperate lua_States
for each entity,
but then I have to register my C Functions for each new entity. This
expensive since we are targetting the console market where memory and
speed are a key requirement.

I've seen some details in the archives but they all use lua_cobegin() a
function that
is no longer available in lua-5.0.

Any tips or even pointers to previous message threads about this would
great. If worse
comes the worse I can create a new lua_State using lua_open() for each
object, but that
seems like too much overhead, what with having to register the C
each time.

Many thanks for your time.

Steven Brekelmans