如何通过一个for循环从C函数返回一个表的表到Lua [英] How to return a table of tables to Lua from C function, through a for loop

查看:142
本文介绍了如何通过一个for循环从C函数返回一个表的表到Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 std :: list 的对象,我想给Lua一个函数,返回它的多维位置。所以我需要创建一个表表:

I have a std::list of objects, and I want to give Lua a function that returns its multidimensional positions. So I need to create a table of tables:

{{1,2,3...,512}, {1,2,3...,512},...,512}

lua_newtable(L);

    for (int i = 0; i < 512; i++)
    {
        lua_newtable(L);

        lua_pushnumber(L, pos[i]);
        lua_rawseti(L, -2, i);
        lua_rawseti(L, -2, i+1);

        for (int j = 0; j < 512; j++)
        {
                //pos[i][j]
        }
    }

我会尝试通过尝试和错误,但因为我不知道如何调试它

I'd try by trial and error, but since I don't know how to debug it for now, I'm really lost.

推荐答案

我想要创建512x512尺寸的嵌套表格/ p>

I think you want to create nested tables (or matrix) with 512x512 dimension.

static int CreateMatrix( lua_State *L ) {
    lua_newtable( L );
    for( int i = 0; i < 512; i++ ) {
        lua_pushnumber( L, i + 1 );    // parent table index
        lua_newtable( L );             // child table
        for( int j = 0; j < 512; j++ ) {
            lua_pushnumber( L, j + 1 );  // this will be the child's index
            lua_pushnumber( L, j + 1 );  // this will be the child's value
            lua_settable( L, -3 );
        }
        lua_settable( L, -3 );
    }
    return 1;
}

您当然可以使用自己的值/索引。

You can of course, use your own values/indexes.

这篇关于如何通过一个for循环从C函数返回一个表的表到Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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