[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Multiple Scripts, Pausing, Threads, and Persistant Varia
- From: Steve Goodrich <steveg@...>
- Date: Tue, 21 Mar 2000 08:50:22 -0700
lua-l@tecgraf.puc-rio.br wrote:
[snip]
> Ick, I'm afraid that won't help... I'd need them to share information...
> ::sigh:: And no, LuaMT is based on Lua 3.1. ::bigger sigh::
>
Well, I can tell you what I did. I have a lua state that is my "global
state". I then create a table in that state (actually, I create four
tables but that is for an implementation specific reason) with a line of
code like:
lua_dostring("globals={}");
Then I create a new tag, change the tag for the global table to the new
tag, and override the gettable and settable tags. You need to do the
following in each state you need globals.
globalTag = lua_newtag();
lua_pushobject(lua_getglobal("globals"));
lua_settag(globalTag);
lua_pushcfunction(GetLuaGlobalTable);
lua_settagmethod(globalTag, "gettable");
lua_pushcfunction(SetLuaGlobalTable);
lua_settagmethod(globalTag, "settable");
Where GetGlobalTable and SetGlobalTable are C functions that look
something like this:
void TLuaState::GetGlobalTable(lua_Object table, lua_Object index)
{
// Ok, lua_Objects cannot travel across state boundries. So we need
to
// get everything into their original forms (doubles, strings, etc.)
// along with the names of the table and index.
// Get the table and index names
char *cpTableName;
lua_getobjname(table, &cpTableName);
char *cpIndex = lua_getstring(index);
// switch states
lua_State * oldState = lua_setstate(gpGlobalState);
// Get the table object
table = lua_getglobal(cpTableName);
// Get the value as a string
lua_pushobject(table);
lua_pushstring(cpIndex);
lua_Object retVal = lua_rawgettable();
char *cpVal = lua_getstring(retVal);
lua_setstate(oldState);
// push the value into the local state.
lua_pushstring(cpVal);
}
void TLuaState::SetGlobalTable(lua_Object table, lua_Object index,
lua_Object value)
{
// Get everything as a string.
char *cpTableName;
lua_getobjname(table, &cpTableName);
char *cpIndex = lua_getstring(index);
char *cpVal = lua_getstring(value);
// Switch states
lua_State * oldState = lua_setstate(gpGlobalState);
// Get the table
table = lua_getglobal(cpTableName);
// Store the value in the table
lua_pushobject(table);
lua_pushstring(cpIndex);
lua_pushstring(cpVal);
lua_rawsettable();
lua_setstate(oldState);
}
This, as it stands, has some limitations. You can't share entire tables,
although the code could be modified to allow it with some work. The bits
and pieces are there in the Lua API. In any case, you can now share
global variables between states by saying "globals.foo = 2.3".
I don't know if I explained this very well but I can answer questions if
you have them.
Steve