【发布时间】:2018-12-15 15:37:03
【问题描述】:
我想知道如何用字段和值形成一个 Lua 表,以便我可以将它作为参数从 C++ 传递给 Lua 函数。
我知道如何使用索引形成表格,但我不知道如何从由字段和值组成的表格中。
例如,我想将此表作为 C++ 的参数发送到 Lua 函数。
t = {xpos = 50, ypos = 80, message = 'hello'}
下面的代码是我能得到的最接近的代码,但它只是没有字段名称的索引表。
lua_getglobal(L, "myLuaFunc");
if (lua_type(L, -1) == LUA_TFUNCTION)
{
lua_newtable(L);
lua_pushinteger(L, 1);
lua_pushnumber(L, 50);
lua_pushinteger(L, 2);
lua_pushnumber(L, 80);
lua_pushinteger(L, 3);
lua_pushstring(L, 'hello');
lua_settable(L, -3);
if (lua_pcall(L, 1, 0, 0))
std::cout << "Error : " << lua_tostring(L, -1) << std::endl;
}
lua_pop(L, 1);
【问题讨论】:
-
所以我们可以假设(从示例中),您已经找到 this?
-
@πάνταῥεῖ 我刚刚做了。但我不认为它有帮助。
-
我不是 100% 确定如何将表的 字段名称 传递给 lua,但我猜你可以使用其他一些接口函数。