如何将C++公共变量绑定到lua脚本中以便访问

如何从 Lua 脚本中的一个类中访问变量。

下面是一个示例:

// C++
struct HObject{
    float x, y, z;
    float sx, sy, sz;

    void funcTest(void);
};

// 绑定对象
static bool checkFunctionArgs(lua_State* ls, const char* fname, unsigned int nargs)
{
   // etc
}

HObject* HObject_check(lua_State* ls, int index)
{
    // etc
}

static int HObject_newHObject(lua_State* ls)
{
   // etc
}

static int HObject_destructor(lua_State* ls)
{
   // etc
}

void HTest_register(lua_State* ls)
{
   // etc
}

-- Lua 脚本
local obj = HObject:new() -- 创建实例
obj:funcTest() -- 正常
obj.x = 10 -- 如何绑定?
io.write(obj.x) -- 如何绑定?

我已经链接了类的函数,但缺少变量。

抱歉我的英文不好...

点赞
用户1783976
用户1783976

你想要做的可以通过在PIL中描述的userdata机制来完成,从http://www.lua.org/pil/28.html开始。你已经知道这个了吗?你在obj:FuncTest()旁边写了“好的”,好像你已经成功实现了这一部分。

我将从该页面中给出一些好的示例。它们描述了如何创建一个C结构体

typedef struct NumArray {
  int size;
  double values[1];  /* variable part */
} NumArray;

然后通过在库中注册方法

static const struct luaL_reg arraylib [] = {
  {"new", newarray},
  {"set", setarray},
  {"get", getarray},
  {"size", getsize},
  {NULL, NULL}
};

int luaopen_array (lua_State *L) {
  luaL_openlib(L, "array", arraylib, 0);
  return 1;
}

可以向userdatum添加数据和方法,并从Lua中访问它们。下面使用数组操作符[],需要一点更多的工作,但它们展示了如何实现。

a = array.new(1000)
a[10] = 3.4         -- setarray
print(a[10])        -- getarray   --> 3.4

现在我不确定,但我认为由于userdata是C结构体,它可能没有函数,只有函数指针。因此在C中,你可能需要将'a'的适当成员传递给函数。

2014-03-29 21:21:19
用户1783976
用户1783976

我在我们的项目中找到了一个例子,它可以实现你想要的功能。


typedef struct luaMyValReg {
  const char *name;
  getSetFunction getter;
  getSetFunction setter;
} luaMyValReg;

#define luaL_reg luaL_Reg

typedef struct luaMyTable {
luaL_reg    *functions;
luaL_reg    *methods;
luaMyValReg *values;
luaMyValReg *arrays;
luaL_reg    *array_methods;
} luaMyTable;

static const struct luaMyValReg lib_val[] = {
   { "key1", MyClass::l_getKey1, MyClass::l_setKey1},
 ...
    {NULL, NULL, NULL}
 }

这些都被设置到一个结构中,并传递给一个函数,该函数将设置userdata。

table.methods = (luaL_reg *) &lib_m;
table.values = (luaMyValReg *) &lib_val;
MyClass::initTable(&table, REGNAME, REGID, ARRAY_REGID);

在该函数中,有一个位将创建元表,并添加lib_val数组中的每个字段。LGlobalState是lua_State*指针。

luaL_register(LGlobalState, regname, table->functions);

// Stk: Table
// 创建元表
luaL_newmetatable(LGlobalState, regid);

// Stk: Table metatable
// 注册它的方法,将其留在堆栈的顶部
luaL_register(LGlobalState, NULL, table->methods);

// Stk: Table metatable
// 设置元表
lua_setmetatable(LGlobalState,-2);

// Stk: Table
// 推入元表
lua_getmetatable(LGlobalState, -1);
// Stk: Table metatable

// 将字段添加到元表中
int i = 0;
while (table->values[i].name != NULL)
{
    addMetaField(LGlobalState, table->values[i].name, i);
    i++;
}

// Stk: Table, metatable
lua_pop(LGlobalState, 2);
// Stk: empty

if (lua_gettop(LGlobalState) > 0)
    tngL_error(LGlobalState, "%s inconsistant stack size\n", regname);

下面是添加值的函数。

////////////////////////////////////////////////////////////////////////////////
int LUATask::addMetaField(lua_State *L, const char *pKey, int nIndex)
////////////////////////////////////////////////////////////////////////////////
// 将字段添加到元表中。它必须已经在栈顶
{
// Stk: metatable
lua_pushstring(L, pKey);
lua_pushnumber(L, nIndex);
// Stk: metatable, string, number
// 使用lua_rawset()而不是lua_settable()来避免
// __newindex 调用
lua_rawset(L, -3);
// Stk: metatable
return 1;
}
2014-04-03 02:33:25