Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Objects in Lua (LuaPlus)

Started by kkrz Sep 3, 2011 at 8:33 PM 2 replies 3.3k views
Original Post
kkrz
kkrz
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:


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?
wqking
wqking
From your code, I can only make some guess...

The last three lines, most likely make a global variable in Lua which name is "EventManager".
Though you have registered the meta table, you need an object to access your member function, that's "EventManager".

So in Lua you can use EventManager to access the member functions.
kkrz
kkrz
I thought that once we created the metatable, any table which used it would "inherit" the functions? So why do we need the C++ object or the global variable "EventManager"? Would the functions still be callable without the last 3 lines?
wqking
wqking
Sorry for the late reply.

IMO, a meta table is a little like a class, any table (or user data) that has a meta table will have all functions in that MT. So your "inherit" idea is correct.

How ever, to use the meta table in Lua, Lua needs to know where to get the MT.

Without the last 3 lines, yes, you create a meta table, but it's not bound to any object (table), and Lua can't use.
The last 3 lines binds the MT to an object (user data or table) which named "EventManager", then Lua can use all functions in the meta table via "EventManager"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.