luastate

lua的各种变量放 luastate里面  luastate作为  函数的参数传递

luaobject

luastack

由于这是个静态函数,所以第一个参数就是脚本里传入的参数。如果是成员变量则第一个参数是this对象,第二个参数才是从脚本传入的参数。

int LWindow::subscribeEvent(LuaState* L)

{

       LuaStack  arg(L);

       if (arg[2].IsString()&& arg[3].IsString())

       {

              ScriptFunctorfunctor(arg[3].GetString());

              d_window->subscribeEvent(arg[2].GetString(),Event::Subscriber(functor));

       }

       return0;

}

客户端lua脚本调试   AxTrace(0, 0, "Pitch:%f", obj.fCameraPitch);这样      2011.1.4

=======================================================================

之前认识有错误的地方

一个LuaState代表一个Lua语言的执行环境,一个程序里可以同时存在多个Lua的环境(如果需要的话)

 

INT PlayerPackage::EnumItem(LuaPlus::LuaState* state)
{
LuaStack args(state);

//检查参数
if (!(args[2].IsString()))
{
KLThrow(
"LUA: PlayerPackage::EnumItem Wrong Param1"
);
}
if(nOffIndex >= 0)
{
tActionItem
* pActionItem = CActionSystem::GetMe()->EnumAction(nIndex + nOffIndex, 
NAMETYPE_PACKAGEITEM);
if(pActionItem)
{
LuaObject objReturn
= state->BoxPointer(pActionItem);
objReturn.SetMetaTable(
*
CActionItem::s_pMetaTable);
objReturn.PushStack();

if(((tObject_Item*)pActionItem->GetImpl())->IsLocked())
state
->PushInteger(1);
else
state
->PushInteger(0);
return 2;
}
}

传参的那种用法

另外复杂的:

LUA_API void lua_newuserdatabox (lua_State *L, void *ptr) {
Udata
*u;
lua_lock(L);
u
= luaS_newudata(L, 4);
u
->uv.len = 1; // user data box bit is set.
*(void**)(u + 1) = ptr;
setuvalue(L
->top, u);
api_incr_top(L);
lua_unlock(L);
}
LUAPLUS_INLINE LuaStackObject LuaState::BoxPointer(void* u)
{
lua_boxpointer(m_state, u);
return LuaStackObject(*this, lua_gettop(m_state));
}
#define lua_boxpointer(L,u) \
(lua_newuserdatabox(L, (u)))
Udata *luaS_newudata (lua_State *L, size_t s) {
Udata
*u;
s
= (s+1) & (~(size_t)1); /* make sure size is multiple of 2 */
u
= cast(Udata *, luaM_malloc(L, sizeudata(s)));
u
->uv.marked = (1<<1); /* is not finalized */
u
->uv.tt = LUA_TUSERDATA;
#if LUA_REFCOUNT
u
->uv.ref = 0;
#endif /* LUA_REFCOUNT */
u
->uv.len = s;
u
->uv.metatable = hvalue(defaultmetatypes(L, LUA_TUSERDATA));
lua_addreftable(u
->uv.metatable);
/* chain it on udata list */
#if LUA_REFCOUNT
u
->uv.prev = (GCObject*)&G(L)->rootudata;
#endif /* LUA_REFCOUNT */
u
->uv.next = G(L)->rootudata;
G(L)
->rootudata = valtogco(u);
#if LUA_REFCOUNT
if (u->uv.next)
u
->uv.next->uv.prev = valtogco(u);
#endif /* LUA_REFCOUNT */
return u;
}

上面那部分不容易找到的代码,位于依赖库里,(despensy).inl,.c等文件里,原因是c和c++的兼容问题

设置返回值为pActionItem

SetMetaTable设置元表(是pActionItem的)

压栈

然后把block的值(0或者1)压栈