将带有子表的表从C ++传递给Lua函数 [英] Passing table with subtable to Lua function from C++

查看:103
本文介绍了将带有子表的表从C ++传递给Lua函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有子表的表作为C ++的参数传递给Lua函数.

I'm trying to pass table with subtable to Lua function as argument from C++.

这是我的代码行不通,但显示了我要执行的操作.

Here's my code that doesn't work but shows what I'm trying to do.

    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中,我希望能够.

And in Lua, I want to be able to, for example..

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 ++函数,使其像示例一样正常工作?

How should I correct my C++ function so it works properly like the example?

推荐答案

这很简单.只需创建子表并将其分配给最外面的表中的字段即可.

It is rather simple. Just create subtables and assign those to fields in the outermost table.

如果dragged不是函数而不是不执行任何操作,我还建议您引发错误.

I also recommend that you raise an error if dragged is not a function rather than doing nothing.

// 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);
    }
}

这篇关于将带有子表的表从C ++传递给Lua函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆