Game Development Reference
In-Depth Information
how to translate the
Process*
parameter. A special
ScriptAttachChild()
is
written to manually perform the translation:
void ScriptProcess::ScriptAttachChild(LuaPlus::LuaObject child)
{
if (child.IsTable())
{
LuaPlus::LuaObject obj = child.GetByName(
“
__object
”
);
if (!obj.IsNil())
{
// Casting a raw ptr to a smart ptr is generally bad, but Lua has no
// concept of what a shared_ptr is. There
'
s no easy way around it.
shared_ptr<Process> pProcess(
static_cast<Process*>(obj.GetLightUserData()));
GCC_ASSERT(pProcess);
AttachChild(pProcess);
}
else
{
GCC_ERROR(
“
Attempting to attach child with no valid object
”
);
}
}
else
{
GCC_ERROR(
Invalid object type passed into \
ScriptProcess::ScriptAttachChild(); type =
“
“
+
std::string(child.TypeName()));
}
}
This function first makes sure the
child
parameter is a table. Then it tries to find
the
__object
field in that table (or the table
s metatable). Remember that the
__object
field is a light userdata field that contains the pointer to the C++
Pro-
cess
object. This is the object that needs to actually be attached. This pointer is
cast into a
Process
smart pointer and attached. Casting a raw pointer into a smart
pointer isn
'
t ideal since Lua still holds onto the raw pointer, but it should be safe
since the
__object
field is destroyed when the C++
Process
object is destroyed.
Functions like
IsAlive()
and
IsDead()
are declared as
const
, which LuaPlus
doesn
'
t know how to handle. Simple non-
const
wrappers are created.
The
CreateFromScript()
function is registered as a function on the metatable
that is exported to Lua. This function creates the actual C++ and Lua instances and
binds them together through the
__object
field:
'