Original Post
hi,
I'm a beginner to Lua/LuaPlus and I've just started learning. I'm looking at the following code and want to be sure that I'm following along correctly. The following code appears in a constructor:
We create a metatable, set its __index value to the metatable itself, and then register some C++ functions as entries within the metatable. What this means in a nutshell is that any table which uses this metatable will essentially "inherit" and be able to use these functions just by calling them, correct?
I'm not sure I understand what's happening in the last 3 lines of code. We associate a LuaObject with the C++ object using this pointer, set its metatable to above, and then create a Lua variable called "EventManager" to access it in Lua? Is "EventManager" the equivalent of a global variable in Lua? What is the point of this if we've already registered the member functions in the metatable?
Why would we ever need to access the C++ object in Lua if we can just use the Register() functions to give scripters access to member functions?
I'm a beginner to Lua/LuaPlus and I've just started learning. I'm looking at the following code and want to be sure that I'm following along correctly. The following code appears in a constructor:
m_MetaTable = g_pApp->m_pLuaStateManager->GetGlobalState()->GetGlobals().CreateTable("EventManager");
m_MetaTable.SetObject("__index", m_MetaTable);
m_MetaTable.RegisterObjectDirect( "TriggerEvent", (EventManager *)0, &EventManager::TriggerEventFromScript );
m_MetaTable.RegisterObjectDirect( "RegisterEventType", (EventManager *)0, &EventManager::RegisterScriptEventType );
m_MetaTable.RegisterObjectDirect( "AddScriptListener", (EventManager *)0, &EventManager::AddScriptListener );
m_MetaTable.RegisterObjectDirect( "RemoveScriptListener", (EventManager *)0, &EventManager::RemoveScriptListener );
m_MetaTable.RegisterObjectDirect( "AddScriptActorListener", (EventManager *)0, &EventManager::AddScriptActorListener );
m_MetaTable.RegisterObjectDirect( "RemoveScriptActorListener", (EventManager *)0, &EventManager::RemoveScriptActorListener );
LuaPlus::LuaObject luaStateManObj = g_pApp->m_pLuaStateManager->GetGlobalState()->BoxPointer(this);
luaStateManObj.SetMetaTable(m_MetaTable);
g_pApp->m_pLuaStateManager->GetGlobalState()->GetGlobals().SetObject("EventManager", luaStateManObj);
We create a metatable, set its __index value to the metatable itself, and then register some C++ functions as entries within the metatable. What this means in a nutshell is that any table which uses this metatable will essentially "inherit" and be able to use these functions just by calling them, correct?
I'm not sure I understand what's happening in the last 3 lines of code. We associate a LuaObject with the C++ object using this pointer, set its metatable to above, and then create a Lua variable called "EventManager" to access it in Lua? Is "EventManager" the equivalent of a global variable in Lua? What is the point of this if we've already registered the member functions in the metatable?
Why would we ever need to access the C++ object in Lua if we can just use the Register() functions to give scripters access to member functions?