How to return an array as table from C function to lua? -
i need impelement c function lua script call.i shall return array table function.i used code blow crashed.could tell me how 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); (int = 0; 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; // want return lua table :{{11, 12}, {21, 22}, {31, 32}} }
need change lua_settable
@lhf mentions. also, adding first 2 indices of 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); (int = 0; < 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; // want return lua table :{{11, 12}, {21, 22}, {31, 32}} }
Comments
Post a Comment