Game Development Reference
In-Depth Information
// convert the pointer to an unsigned long to use as the handle
unsigned long handle = reinterpret_cast<unsigned long>(pListener);
return handle;
}
GCC_ERROR(
“
Attempting to register script event listener with \
invalid callback function
”
);
return 0;
}
After a bit of error checking, this function creates a new
ScriptEventListener
object and adds it to the
ScriptEventListenerMgr
instance. Then it registers
the newly created C++ delegate proxy with the Event Manager.
With all the pieces in place, it is now possible to create events that can be sent across
the C++/Lua boundary. To do this, create a new event class that inherits from
ScriptEvent
. Then implement the
BuildEventData()
and
BuildEventFrom-
Script()
virtual methods as necessary. Call the
EXPORT_FOR_SCRIPT_EVENT()
macro in the event subclass declaration and call the
REGISTER_SCRIPT_EVENT()
macro in the initialization code for the application.
To test out this functionality, we create two simple events. One is sent from C++ and
received by Lua, the other is sent from Lua and received by C++. Both of these events
inherit from
ScriptEvent
and override the appropriate virtual functions. Here are
the overridden functions:
// This is for the event being sent from C++ to Lua
void EvtData_ScriptEventTest_ToLua::BuildEventData(void)
{
m_eventData.AssignNumber(LuaStateManager::Get()->GetLuaState(), m_num);
}
// This is for the event being sent from Lua to C++
bool EvtData_ScriptEventTest_FromLua::BuildEventFromScript(void)
{
if (m_eventData.IsInteger())
{
m_num = m_eventData.GetInteger();
return true;
}
return false;
}