I'm trying to add luabind to my engine which currently uses plain C lua to implement a console interpreter. At this stage I simply want to use luabind to easily bind console commands to functions. I'm currently however having problems running my existing code for overloading the __index and __newindex methods with luabind - the application crashes and I'm unable to figure out why except that if I remove the overloading code, the crash goes away and the binding works fine, but as a result I can no longer bind console variables.
This is my initialization code, take note of big NOTE:
[source lang=cpp]
static const char* Z_TABLE = "_G";
...
m_pLuaState = lua_open();
luaL_openlibs( m_pLuaState );
g_pLuaState = m_pLuaState;
luabind::open(m_pLuaState);
// Make sure that the table with name Z_TABLE exists.
// This table will be the "proxy" table for getting and setting the console variables,
lua_getglobal(m_pLuaState, Z_TABLE);
if (!lua_istable(m_pLuaState, -1))
{
if (!lua_isnil(m_pLuaState, -1))
{
// No table at name Z_TABLE, but something else??
}
lua_pop(m_pLuaState, 1); // Pop whatever it was.
lua_newtable(m_pLuaState); // Create a new table.
lua_pushvalue(m_pLuaState, -1); // Pushes/duplicates the new table on the stack.
lua_setglobal(m_pLuaState, Z_TABLE); // Pop the copy and set it as a global variable with name Z_TABLE.
}
// Create a new table T and add it into the registry table with "ConVars_Mediator" as the key and T as the value.
// This also leaves T on top of the stack. T will be used as the metatable for the Z_TABLE.
luaL_newmetatable(m_pLuaState, "ConVars_Mediator");
static const luaL_reg MediatorMethods[]=
{
{ "__index", Lua_get_Callback },
{ "__newindex", Lua_set_Callback },
{ NULL, NULL }
};
// Insert the functions listed in MediatorMethods into T (the table on top of the stack).
luaL_register(m_pLuaState, NULL, MediatorMethods);
// <<<<<<<<<<<< NOTE NOTE NOTE NOTE NOTE >>>>>>>>>>>
// If I return here then luabind doesn't crash anymore when trying to bind.
// But obivously now console vars are broken :/
// return;
// Now set T as the metatable of Z_TABLE, "Z_TABLE.__metatable=T;".
// This removes T from the stack, leaving only the Z_TABLE.
lua_setmetatable(m_pLuaState, -2);
lua_pushlightuserdata( m_pLuaState, (void *)&LuaRegistryGUID );
lua_pushlightuserdata( m_pLuaState, this );
lua_settable( m_pLuaState, LUA_REGISTRYINDEX );
[/source]
Is this a problem with luabind not able to handle this, or is there another way to both functionality?
Thanks!