lua-users home
lua-l archive

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

OOOoooo... sounds too easy!! I'll try it out. Cheers!

Marcus Brown wrote:

* David Morris-Oliveros <> on Mon, Oct 10, 2005:


I'm on my next stage of integrating lua into our engine, and have come to a point where it would be wise to ask for advice.

I want each coroutine to have their own individual global table. I know that you can do this by creating a new table, filling it with whatever you want, and doing a lua_setfenv().

Now, when I bind my code into lua, i create all sorts of tables and cfunctions in the global table. I still want these new coroutines to be able access my bound tables and cfunctions.

I'm doing the same exact thing in our project - creating a seperate global
namespace for threads with access to the real global table.

Just create your thread's global table, and give it a metatable with the
__index metamethod pointing to the global table:

   namespace = setmetatable({}, {__index = _G})

In C this would look something like:

   int metatable = lua_gettop(L);

   lua_pushvalue(L, LUA_GLOBALSINDEX);
   lua_setfield(L, metatable, "__index");    /* {__index = _G} */

Then set this as the metatable of your namespace.

I believe this was mentioned in PIL, section 14.3.

// David Morris-Oliveros
// Camera & Lua Coder
// Team Bondi

Team Bondi Pty Ltd
Level 2, 608 Harris Street
Ultimo, NSW 2007
Tel: +61 (0)2 8218 1500
Fax: +61 (0)2 8218 1507
This email may contain confidential information.  If you are not
the intended recipient, you may not copy or deliver this message to
anyone. In such case, you should destroy this message and kindly
notify the sender by reply email. Opinions, conclusions and other
information in this message that do not relate to the official business
of our firm shall be understood as neither given nor endorsed by it.