Transfer multidimensional lua array from lua_State to lua_State C++ -
i have 2 scripts, each in different lua_state.
i trying able variable 1 state , use in other.
my code below works single variables , unidirectional arrays. guidance on making work multidimensional arrays?
void getvalues(lua_state* l1, lua_state* l2, int& returns) { if (lua_isuserdata(l1, -1)) { luaelement* e = luna<luaelement>::to_object(l1, -1); if (e != null) { luna<luaelement>::push_object(l2, e); } } else if (lua_isstring(l1, -1)) { lua_pushstring(l2, lua_tostring(l1, -1)); } else if (lua_isnumber(l1, -1)) lua_pushnumber(l2, lua_tonumber(l1, -1)); else if (lua_isboolean(l1, -1)) lua_pushboolean(l2, lua_toboolean(l1, -1)); else if (lua_istable(l1, -1)) { lua_pushnil(l1); lua_newtable(l2); while (lua_next(l1, -2)) { getvalues(l1, l2, returns); lua_rawseti(l2,-2,returns-1); lua_pop(l1, 1); } // lua_rawseti(l2,-2,returns); // needs work } returns++; }
unfortunately i'm having tough time getting recursion right work multidimensional arrays.
solved.
for might useful:
void getvalues(lua_state* l1, lua_state* l2, int ind) { if (lua_type(l1, -1) == lua_ttable) { lua_newtable(l2); lua_pushnil(l1); ind = 0; while (lua_next(l1, -2)) { // push key if (lua_type(l1, -2) == lua_tstring) lua_pushstring(l2, lua_tostring(l1, -2)); else if (lua_type(l1, -2) == lua_tnumber) lua_pushnumber(l2, lua_tonumber(l1, -2)); else lua_pushnumber(l2, ind); getvalues(l1, l2, ind++); lua_pop(l1, 1); lua_settable(l2, -3); } } else if (lua_type(l1, -1) == lua_tstring) { lua_pushstring(l2, lua_tostring(l1, -1)); } else if (lua_type(l1, -1) == lua_tnumber) { lua_pushnumber(l2, lua_tonumber(l1, -1)); } else if (lua_type(l1, -1) == lua_tboolean) { lua_pushboolean(l2, lua_toboolean(l1, -1)); } else if (lua_type(l1, -1) == lua_tuserdata) { // replace own user data. mine luaelement* e = luna<luaelement>::to_object(l1, -1); if (e != null) { luna<luaelement>::push_object(l2, e); } } }
warning: l1 , l2 must different states.
Comments
Post a Comment