Game Development Reference
In-Depth Information
// call the Lua function
shared_ptr<ScriptEvent> pScriptEvent =
static_pointer_cast<ScriptEvent>(pEvent);
LuaPlus::LuaFunction<void> callback = m_scriptCallbackFunction;
callback(pScriptEvent->GetEventData());
}
All this function does is calls the Lua delegate function with the results of
GetEventData()
as the only parameter.
GetEventData()
calls your script event
s
BuildEventData()
function if necessary and returns
m_eventData
. The Lua del-
egate then does whatever it wants with the data.
These event listeners take care of all the overhead for binding the Lua listener dele-
gate to a C++ delegate. The
ScriptEventListenerMgr
manages these objects.
'
class ScriptEventListenerMgr
{
typedef std::set<ScriptEventListener*> ScriptEventListenerSet;
ScriptEventListenerSet m_listeners;
public:
˜
ScriptEventListenerMgr(void);
void AddListener(ScriptEventListener* pListener);
void DestroyListener(ScriptEventListener* pListener);
};
This class maintains a set of
ScriptEventListener
objects, which are added and
removed through the
AddListener()
and
DestroyListener()
functions, respec-
tively. These functions are just wrappers to insert or remove/delete objects from the
set.
The final piece we need is a function for registering a Lua event listener, which is a
function that is exposed to Lua.
unsigned long InternalScriptExports::RegisterEventListener(EventType eventType,
LuaPlus::LuaObject callbackFunction)
{
GCC_ASSERT(s_pScriptEventListenerMgr);
if (callbackFunction.IsFunction())
{
// create the C++ listener proxy and set it to listen for the event
ScriptEventListener* pListener = GCC_NEW ScriptEventListener(eventType,
callbackFunction);
s_pScriptEventListenerMgr->AddListener(pListener);
IEventManager::Get()->VAddListener(pListener->GetDelegate(), eventType);