Game Development Reference
In-Depth Information
LuaPlus::LuaObject ScriptProcess::CreateFromScript(LuaPlus::LuaObject self,
LuaPlus::LuaObject constructionData,
LuaPlus::LuaObject originalSubClass)
{
// Note: The self parameter is not used in this function but it allows us
// to be consistent when calling Create(). The Lua version of this function
// needs self.
ScriptProcess* pObj = GCC_NEW ScriptProcess;
pObj->m_self.AssignNewTable(LuaStateManager::Get()->GetLuaState());
if (pObj->BuildCppDataFromScript(originalSubClass, constructionData))
{
LuaPlus::LuaObject metaTableObj =
LuaStateManager::Get()->GetGlobalVars().Lookup(SCRIPT_PROCESS_NAME);
GCC_ASSERT(!metaTableObj.IsNil());
pObj->m_self.SetLightUserData(
“
__object
”
, pObj);
pObj->m_self.SetMetaTable(metaTableObj);
}
else
{
pObj->m_self.AssignNil(LuaStateManager::Get()->GetLuaState());
SAFE_DELETE(pObj);
}
return pObj->m_self;
}
The first parameter is just to allow consistency so the function can be called in Lua
with the colon operator, just like the
Create()
functions for other Lua classes using
the
class()
function. The second parameter is the construction data, and the third
parameter is the original subclass this object is being instantiated from. These para-
meters are exactly the same as the three parameters in the
Create()
function
attached to classes through the
class()
function you saw earlier in this chapter.
This is no coincidence; the functions should be completely interchangeable so that
the caller has no idea if it
s creating a C++ object or a pure Lua object.
Inside the function, the C++ object is instantiated, followed by the creation of the
Lua table that will serve as the instance object. It
'
s created on the
m_self
member
so that the C++ object always has a reference to the Lua object, just like the Lua
object has a reference to the C++ object through the
__object
field. Next, the func-
tion calls
BuildCppDataFromScript()
, which mines the
constructionData
and
originalSubClass
tables for any functions and configuration data that are
appropriate (see below). If this succeeds, the function finds the metatable that was
'