I would like improve my code below by sending a C++ Pre-formatted Lua Table:
int GetCategory(lua_State* L)
{
uint32 Type = CHECKVAL<int>(L, 1);
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 counter = 1;
// Struct CT { string CategoryBrandName, CategoryName }; > Vector<CT>
auto list = sManagerMgr->GetAll();
// Hack modify this to send a metatable/UserData/Table whatever is called
for (auto& elem : list)
{
switch (Type)
{
case 1:
lua_pushstring(L, elem->CategoryBrandName);
break;
case 2:
lua_pushstring(L, elem->CategoryName);
break;
}
lua_rawseti(L, tbl, counter);
counter++;
}
lua_settop(L, tbl);
return 1;
}
Basically, lua_newtable pushes a table to the lua stack, lua_gettop will take the top index, so the index where the table is at. Then lua_pushstring(L, ELEMENT); lua_rawseti(L, tbl, counter); will put the ELEMENT to the table at the index tbl we got with gettop. The index of the element is the value of counter.
But The issue here is that i'm forced to call twice the fonction GetCategory to fill it as follow in my .lua file.
table.insert(Group, { GetCategory(1), GetCategory(2) });
Current Use :
print(i, Group(1)[i], Group(2)[i]);
So.. I would prefer to call it once and get something like this directly :
local Group =
{
[1] = { "elem->CategoryBrandName[1]", "elem->CategoryName[1]" },
[2] = { "elem->CategoryBrandName[2]", "elem->CategoryName[2]" }
--etc
};
I've tried filling elem into an 2D Array[1][2] and then pushing Array unsuccessfully
I've made a lot of research about Table, Metatables, MultiDimentional Arrays etc but I couldn't find something that would fit my need or works.
Does anyone has a solution ?