I learned how to redefine the Lua's print()
in C++ from this post. (https://mcmap.net/q/631350/-redirecting-redefining-print-for-embedded-lua)
Here's the redefined print function that prints variables to my host program's console. (through the functions named post..
)
int l_my_print(lua_State *L)
{
int nargs = lua_gettop(L);
for (int i = 1; i <= nargs; ++i)
{
if (lua_isnil(L, i))
poststring("nil");
else if (lua_isboolean(L, i))
lua_toboolean(L, i) ? poststring("true") : poststring("false");
else if (lua_isnumber(L, i))
postfloat(static_cast<t_float>(lua_tonumber(L, i)));
else if (lua_isstring(L, i))
poststring(lua_tostring(L, i));
else if (lua_istable(L, i))
poststring("table: "); //how to print like Lua's built-in print()?
}
endpost();
return 0;
}
This code works fine except when I print tables. (it just prints table:
now)
I wonder how to print tables just like how Lua's print()
works.
For example, when I run the following code in Lua: (before the redefine)
print({1,2,3});
I get this result: (which seems to change constantly)
table: 0x23b8660
Is this a hex representation of pointer to the Lua-table?
What should I do with my l_my_print()
function so it can work just like Lua's print()
?
lua_tostring
on the table too. – Breezeprint
to work just like the original one. – Sardinia