c++ - Lua return custom data from C function -


despite searching hard, couldn't find valid lua c api example calling lua function returning custom data c function. example, have register function "getmyvector" , i'm calling lua retrive informations c, got table want access access variable struct in c, example:

local x = getmyvector() print(x[1]) --  print(x[2]) -- j print(x[3]) -- k -- how access via this: print(x.i) print(x.j) print(x.k) 

my c function pushing vector in 3 dimensional array lua_pushnumber:

static int getmyvector(lua_state *l) {     vec3_t vec;     vec[0] = 1;     vec[1] = 2;     vec[3] = 3;     lua_newtable(l);     lua_pushnumber(l, vec[0]);     lua_rawseti(l, -2, 1);     lua_pushnumber(l, vec[1]);     lua_rawseti(l, -2, 2);     lua_pushnumber(l, vec[2]);     lua_rawseti(l, -2, 3);     return 1; } 

you want lua_settable. allows set keys table. if key literal can data x["i"] or x.i . code should like

static int getmyvector(lua_state *l) {     vec3_t vec;     vec[0] = 1;     vec[1] = 2;     vec[3] = 3;     lua_newtable(l);      lua_pushliteral(l, "i");     lua_pushnumber(l, vec[0]);     lua_settable(l, -2);      lua_pushliteral(l, "j");     lua_pushnumber(l, vec[1]);     lua_settable(l, -2);      lua_pushliteral(l, "k");     lua_pushnumber(l, vec[2]);     lua_settable(l, -2);     return 1; } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -