将字段作为表从C++传入Lua函数? 我想知道如何用字段和值来形成一个LUA表,这样我就可以把它作为一个参数传递给C++的Lua函数。 t = {xpos = 50, ypos = 80, message = 'hello'}
我知道如何使用索引形成一个表,但我不知道如何从由字段和值组成的表中创建索引将字段作为表从C++传入Lua函数? 我想知道如何用字段和值来形成一个LUA表,这样我就可以把它作为一个参数传递给C++的Lua函数。 t = {xpos = 50, ypos = 80, message = 'hello'},c++,lua,lua-table,C++,Lua,Lua Table,我知道如何使用索引形成一个表,但我不知道如何从由字段和值组成的表中创建索引 例如,我想把这个表作为一个参数从C++中发送到一个LUA函数。 t = {xpos = 50, ypos = 80, message = 'hello'} 下面的代码是我能得到的最接近的代码,但它只是一个没有字段名的索引表 lua_getglobal(L, "myLuaFunc"); if (lua_type(L, -1) == LUA_TFUNCTION) { lua_newtable(L); 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);
我不确定我是否正确理解了这个问题。如果希望字符串作为表中的键,则只需按字符串而不是数字
#include <iostream>
#include <lua.hpp>
int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
char const script[] = "function test(t)\n"
" print(t.xpos)\n"
" print(t.ypos)\n"
" print(t.message)\n"
"end";
if (luaL_dostring(L, script) != 0) {
std::cerr << lua_tostring(L, -1) << '\n';
lua_close(L);
return 1;
}
lua_getglobal(L, "test");
if (lua_isfunction(L, -1)) {
lua_newtable(L);
// xpos = 50
lua_pushstring(L, "xpos");
lua_pushinteger(L, 50);
lua_settable(L, -3);
// ypos = 80
lua_pushstring(L, "ypos");
lua_pushinteger(L, 80);
lua_settable(L, -3);
// message = "hello"
lua_pushstring(L, "message");
lua_pushstring(L, "hello");
lua_settable(L, -3);
if (lua_pcall(L, 1, 0, 0) != 0) {
std::cerr << "lua:" << lua_tostring(L, -1) << '\n';
lua_close(L);
return 1;
}
}
lua_close(L);
}
我不确定我是否正确理解了这个问题。如果希望字符串作为表中的键,则只需按字符串而不是数字
#include <iostream>
#include <lua.hpp>
int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
char const script[] = "function test(t)\n"
" print(t.xpos)\n"
" print(t.ypos)\n"
" print(t.message)\n"
"end";
if (luaL_dostring(L, script) != 0) {
std::cerr << lua_tostring(L, -1) << '\n';
lua_close(L);
return 1;
}
lua_getglobal(L, "test");
if (lua_isfunction(L, -1)) {
lua_newtable(L);
// xpos = 50
lua_pushstring(L, "xpos");
lua_pushinteger(L, 50);
lua_settable(L, -3);
// ypos = 80
lua_pushstring(L, "ypos");
lua_pushinteger(L, 80);
lua_settable(L, -3);
// message = "hello"
lua_pushstring(L, "message");
lua_pushstring(L, "hello");
lua_settable(L, -3);
if (lua_pcall(L, 1, 0, 0) != 0) {
std::cerr << "lua:" << lua_tostring(L, -1) << '\n';
lua_close(L);
return 1;
}
}
lua_close(L);
}
您还可以使用lua_setfield,这使代码更短,可能更易于阅读:
lua_newtable(L);
lua_pushinteger(L, 50); // xpos = 50
lua_setfield(L, -2, "xpos");
lua_pushinteger(L, 80); // ypos = 80
lua_setfield(L, -2, "ypos");
lua_pushstring(L, "hello"); // message = "hello"
lua_setfield(L, -2, "message");
您还可以使用lua_setfield,这使代码更短,可能更易于阅读:
lua_newtable(L);
lua_pushinteger(L, 50); // xpos = 50
lua_setfield(L, -2, "xpos");
lua_pushinteger(L, 80); // ypos = 80
lua_setfield(L, -2, "ypos");
lua_pushstring(L, "hello"); // message = "hello"
lua_setfield(L, -2, "message");
因此,我们可以从示例中假设,您已经找到了?@πάνταῥεῖ 我就这么做了。但我不认为这有什么帮助。我不是100%确定一个表的字段名是如何传递给lua的,但我想你可以使用其他接口函数。因此,我们可以从示例中假设,你已经找到了?@πάνταῥεῖ 我就这么做了。但我不认为这有什么帮助。我不是100%确定表的字段名是如何传递给lua的,但我想你可以使用其他接口函数。太好了!这正是我想知道的。非常感谢您,先生。@ZackLee您也可以阅读文档。@ZackLee,看看我的答案。你们能检查一下我的其他问题吗?伟大的这正是我想知道的。非常感谢您,先生。@ZackLee您也可以阅读文档。@ZackLee,看看我的答案。你们能检查一下我的其他问题吗?