I need impelement a C function for lua script to call.I shall return an array as table from that function.I used the code blow but crashed.Could anyone tell me how to use it?
struct Point {
int x, y;
}
typedef Point Point;
static int returnImageProxy(lua_State *L)
{
Point points[3] = {{11, 12}, {21, 22}, {31, 32}};
lua_newtable(L);
for (int i = 0; i 3; i++) {
lua_newtable(L);
lua_pushnumber(L, points[i].x);
lua_rawseti(L, -2, 2*i+1);
lua_pushnumber(L, points[i].y);
lua_rawseti(L, -2, 2*i+2);
lua_settable(L,-3);
}
return 1; // I want to return a Lua table like :{{11, 12}, {21, 22}, {31, 32}}
}
Need to change the lua_settable
as @lhf mentions. Also, you are always adding into the first 2 indices of the sub-tables
typedef struct Point {
int x, y;
} Point;
static int returnImageProxy(lua_State *L)
{
Point points[3] = {{11, 12}, {21, 22}, {31, 32}};
lua_newtable(L);
for (int i = 0; i < 3; i++) {
lua_newtable(L);
lua_pushnumber(L, points[i].x);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, points[i].y);
lua_rawseti(L, -2, 2);
lua_rawseti(L, -2, i+1);
}
return 1; // I want to return a Lua table like :{{11, 12}, {21, 22}, {31, 32}}
}
尝试用lua_settable(L,-3)
lua_rawseti(L,-2,i+1)
替换lua_settable(L,-3)
lua_rawseti(L,-2,i+1)
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.