从 C++ 将 table 与 subtable 传递给 Lua 函数
Passing table with subtable to Lua function from C++
我正在尝试将 table 与 subtable 传递给 Lua 函数作为 C++ 的参数。
这是我的代码,它不起作用但显示了我正在尝试做的事情。
class DragInfo{
public:
std::vector <std::string> files;
glm::vec2 position;
};
//a callback that passes DragInfo to a Lua function as a table which has 2 subtables
void callDragged(DragInfo &info)
{
lua_getglobal(L, "dragged");
if (!lua_isfunction(L, -1))
{
lua_pop(L, 1);
return;
}
lua_newtable(L);
for (size_t i = 0; i < info.files.size(); ++i)
{
lua_pushinteger(L, static_cast<lua_Integer>(i + 1));
lua_pushstring(L, info.files[i].c_str());
lua_settable(L, -3);
}
lua_pushnumber(L, static_cast<lua_Number>(info.position.x));
lua_setfield(L, -2, "x");
lua_pushnumber(L, static_cast<lua_Number>(info.position.y));
lua_setfield(L, -2, "y");
if (lua_pcall(L, 1, 0, 0))
std::cout << "Error : " << lua_tostring(L, -1) << std::endl;
}
并且在 Lua 中,我希望能够,例如..
function dragged(info)
for i=1, #info.files do
print("dragged filename: " .. info.files[i])
end
print("dragged position: " .. info.position.x .. " " .. info.position.y)
end
结果可能类似于
dragged filename: background.jpg
dragged filename: apple.png
dragged position: 425 32
我应该如何更正我的 C++ 函数,使其像示例一样正常工作?
比较简单。只需创建子 table 并将它们分配给最外层 table.
中的字段
我还建议您在 dragged
不是函数时引发错误,而不是什么都不做。
// a callback that passes DragInfo to a Lua function as a table which has 2
// subtables
void callDragged(DragInfo &info) {
lua_getglobal(L, "dragged");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
lua_pushstring(L, "argument is not a function");
lua_error(L);
return;
}
// outermost table
lua_newtable(L);
// subtable "files"
lua_newtable(L);
for (size_t i = 0; i < info.files.size(); ++i) {
lua_pushinteger(L, i + 1);
lua_pushstring(L, info.files[i].c_str());
lua_settable(L, -3);
}
lua_setfield(L, -2, "files");
// subtable "position"
lua_newtable(L);
lua_pushnumber(L, info.position.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, info.position.y);
lua_setfield(L, -2, "y");
lua_setfield(L, -2, "position");
if (lua_pcall(L, 1, 0, 0) != 0) {
std::cout << "Error : " << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
}
}
我正在尝试将 table 与 subtable 传递给 Lua 函数作为 C++ 的参数。
这是我的代码,它不起作用但显示了我正在尝试做的事情。
class DragInfo{
public:
std::vector <std::string> files;
glm::vec2 position;
};
//a callback that passes DragInfo to a Lua function as a table which has 2 subtables
void callDragged(DragInfo &info)
{
lua_getglobal(L, "dragged");
if (!lua_isfunction(L, -1))
{
lua_pop(L, 1);
return;
}
lua_newtable(L);
for (size_t i = 0; i < info.files.size(); ++i)
{
lua_pushinteger(L, static_cast<lua_Integer>(i + 1));
lua_pushstring(L, info.files[i].c_str());
lua_settable(L, -3);
}
lua_pushnumber(L, static_cast<lua_Number>(info.position.x));
lua_setfield(L, -2, "x");
lua_pushnumber(L, static_cast<lua_Number>(info.position.y));
lua_setfield(L, -2, "y");
if (lua_pcall(L, 1, 0, 0))
std::cout << "Error : " << lua_tostring(L, -1) << std::endl;
}
并且在 Lua 中,我希望能够,例如..
function dragged(info)
for i=1, #info.files do
print("dragged filename: " .. info.files[i])
end
print("dragged position: " .. info.position.x .. " " .. info.position.y)
end
结果可能类似于
dragged filename: background.jpg
dragged filename: apple.png
dragged position: 425 32
我应该如何更正我的 C++ 函数,使其像示例一样正常工作?
比较简单。只需创建子 table 并将它们分配给最外层 table.
中的字段我还建议您在 dragged
不是函数时引发错误,而不是什么都不做。
// a callback that passes DragInfo to a Lua function as a table which has 2
// subtables
void callDragged(DragInfo &info) {
lua_getglobal(L, "dragged");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
lua_pushstring(L, "argument is not a function");
lua_error(L);
return;
}
// outermost table
lua_newtable(L);
// subtable "files"
lua_newtable(L);
for (size_t i = 0; i < info.files.size(); ++i) {
lua_pushinteger(L, i + 1);
lua_pushstring(L, info.files[i].c_str());
lua_settable(L, -3);
}
lua_setfield(L, -2, "files");
// subtable "position"
lua_newtable(L);
lua_pushnumber(L, info.position.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, info.position.y);
lua_setfield(L, -2, "y");
lua_setfield(L, -2, "position");
if (lua_pcall(L, 1, 0, 0) != 0) {
std::cout << "Error : " << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
}
}