Game Development Reference
In-Depth Information
created with
RegisterScriptClass()
function. Then it binds the C++ instance to
the Lua instance by setting the
__object
field. Then it sets the metatable. If
BuildCppDataFromScript()
fails, both the table and the C++ object are
destroyed. The
m_self
parameter is returned, which, if successful, will contain the
Lua instance. If the function failed, the return value will be
nil
.
The
BuildCppDataFromScript()
function is responsible for finding all the appro-
priate functions defined in the Lua class table:
bool ScriptProcess::BuildCppDataFromScript(LuaPlus::LuaObject scriptClass,
LuaPlus::LuaObject constructionData)
{
if (scriptClass.IsTable())
{
// OnInit()
LuaPlus::LuaObject temp = scriptClass.GetByName(
“
OnInit
”
);
if (temp.IsFunction())
m_scriptInitFunction = temp;
// OnUpdate()
temp = scriptClass.GetByName(
“
OnUpdate
”
);
if (temp.IsFunction())
{
m_scriptUpdateFunction = temp;
}
else
{
GCC_ERROR(
“
No OnUpdate() found in script process; type ==
”
+
std::string(temp.TypeName()));
return false;
}
// OnSuccess()
temp = scriptClass.GetByName(“OnSuccess”);
if (temp.IsFunction())
m_scriptSuccessFunction = temp;
// OnFail()
temp = scriptClass.GetByName(“OnFail”);
if (temp.IsFunction())
m_scriptFailFunction = temp;
// OnAbort()
temp = scriptClass.GetByName(
“
OnAbort
”
);
if (temp.IsFunction())