lua(1)

来源:互联网 发布:我的世界java官方版 编辑:程序博客网 时间:2024/06/14 03:57

重拾lua的学习~

大概的看了一下,小小的lua用的内容可是不少,boost template auto_ptr policy............

对于 匿名对象 和 各种操作符的重载 简直炉火纯青哇

scope& operator,(scope s)

namespace_& operator[](scope s);

void operator[](scope s)

 

{

 using namespace luabind;

module(L)[

def("funName", &funName),

class_<CGuiNpcDialog>("CGuiNpcDialog").def("funName2", CGuiNpcDialog::funName2).def(......)

]

}

module(L)是一个匿名对象,并且重载了[]操作符(void operator[](scope s))

def返回的是scope对象,scope对象又重载了,操作符(scope& operator,(scope s))并且返回当前对象的引用

class_<CGuiNpcDialog>("CGuiNpcDialog").也是一个匿名对象,并且是继承scope 的,所以可以直接转scope,它的成员函数def也是返回的class_& ,所以也可连续的调用

 

lua的栈的索引可以是 1 2 3 也可以是-1 -2 -3 ,如果是A 是-1 ,push B后 A就下移动变为-2 所以-1是栈顶

 

width =200;

heigh = 300;

void   load(lua_State* L, const char* fname , int* w, int* h)

{

      if  ( luaL_loadfile(L, fname) || lua_pcall(L, 0, 0 ,0 ) )

           error(...)

      lua_getglobal(  L ,  "width");

     lua_getglobal( L, "height" );

      if(  ! lua_isnumber(L,  -2) )

            error( L, "width should be a number");

     if( ! lua_isnumber(L , -1) )

            error(...);

     *w = lua_tointeger( L, -2);

     *h = lua_tointerger(L, -1);
}

这是一段很简单的代码

lua_getglobal 的每一次调用,都会将相应的全局变量值压入栈中,如果栈非空,只能用 负数索引  -1 -2

 

 

 

 

 

 

 

 

 

 

原创粉丝点击