lua-users home
lua-l archive

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


Hi,

I've been making a ps2 game, and I'm using lua to implement AI script.

So far, the info on this maling list has helped me a lot,
But now, I'm experiencing a new problem and I can not find a
solution anywhere.

The problem is that each character in game has his own thread, and these
threads
are inherited from a main thread which is registed with standard functions,
created using lua_newthread.

Each sub thread is created when a new character is created, and loads
AI script to each new thread.

Sub threads have a global variable which are named 'agent' and are exported
by 'tolua' from C++.
C++ and lua are interfaced on 'agent' variable.

But, this schema seems to have the problem of memory consumption
becasuse each individual thread has function chunk.
I then attemped to change the structure for preventing
memory consumption by using the method below:

1. main thread ( registed standard functions )
2. sub thread of character's AI script.
3. character's entity thread. ( global enviroment for each character )

I tried this, but the global variable 'agent' is
not being accessed from the script thread's second layer functions.
So I tried to use setmetatable in the character entity thread to solve this
issue.
But it didn't work.

For instance, I'm using the function below to make a sub thread.

lua_State* createSubThread( lua_State* pBase, int& m_thread_reference ) {

lua_State* pSub;
pSub = lua_newthread( pBase );
m_thread_reference = luaL_ref(pBase, LUA_REGISTRYINDEX);

lua_newtable( pSub );    //new globals table
lua_newtable( pSub );    //metatable
lua_pushliteral( pSub, "__index" );
lua_pushvalue( pSub, LUA_GLOBALSINDEX );  //original globals
lua_settable( pSub, -3 );
lua_setmetatable( pSub, -2 );
lua_replace( pSub, LUA_GLOBALSINDEX );    //replace pSub's globals

return pSub;
}


The character's script is loaded to a new thread created by above the
function.
It also creates a new inherited thread from it.
And I have inserted a global variable 'agent' like below:


lua_pushstring( pSub, "agent" );
tolua_pushusertype( pSub,(void*)pAgent,"CAgent");
lua_settable( pSub, LUA_GLOBALSINDEX);

But it can not be accessed from a script loaded thread.
I have tried to change this and make script threads behave as a class.
See below:

lua_newtable( pSub );    //new globals table
lua_pushvalue( pSub, LUA_GLOBALSINDEX );  //original globals
lua_setmetatable( pSub, -2 );
lua_pushliteral( pSub, "__index" );
lua_pushvalue( pSub, LUA_GLOBALSINDEX );  //original globals
lua_settable( pSub, LUA_GLOBALSINDEX );
lua_replace( pSub, LUA_GLOBALSINDEX );    //replace m_pState's globals}

lua_pushstring( pSub, "agent" );
tolua_pushusertype( pSub,(void*)this,"CAgent");
lua_settable( pSub, LUA_GLOBALSINDEX);

But this didn't work either...

If I give up using global variable, I think that this problem can be
solved.
But I want to solve the with using the global variable because there are a
lot of scripts already.
Is there anyway to solve this?
If you know a way, please tell me.

Regards,
Miyagawa.