游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2725|回复: 1

C++ 如何获得LUA函数返回的表中的数据

[复制链接]

3

主题

6

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2010-8-9 20:52:00 | 显示全部楼层 |阅读模式
[em2]
请问一下,我是在C++中调用LUA中的函数,这个函数返回的数据是一张表,请问我应该如何接收呢?万分感激啊啊 [em15]

0

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2010-8-21 11:16:00 | 显示全部楼层

Re:C++ 如何获得LUA函数返回的表中的数据

void test()
{
        lua_State* L = luaL_newstate();
        luaL_openlibs(L);

        if(luaL_dofile(L, "test.lua"))
                luaL_error(L, "%s", lua_tostring(L, -1));

        lua_getglobal(L, "foo");
        lua_pcall(L, 0, 1, 0);

        lua_pushstring(L, "name");
        lua_gettable(L, -2);

        printf("name = %s,\n", lua_tostring(L, -1));
        lua_pop(L, 1);

        lua_pushstring(L, "age");
        lua_gettable(L, -2);

        printf("age = %d\n", lua_tonumber(L, -1));
        lua_pop(L, 1);

        lua_pop(L, 1);

        lua_close(L);
}

--test.lua
function foo()
t = {
                ["name"] = '小明',
                ["age"]  = 24
        };
return t;
end
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-18 20:25

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表