Game Development Reference
In-Depth Information
// set the event data that was passed in
if (!pEvent->SetEventData(eventData))
{
return shared_ptr<ScriptEvent>();
}
return pEvent;
}
This function creates the event by calling CreateEventFromScript() , which in
turn calls the factory method to instantiate the appropriate subclass. Then it calls
SetEventData() , which will call your implementation of BuildEventFrom-
Script() . If this succeeds, it returns the newly created event. That
s all there is to
it. Using these two methods, you can send events from Lua and have them received
by C++ listeners. In C++, you create those listeners as normal.
Allowing Lua to receive events is a little trickier. We want to create a system where you
can register a Lua function to receive a C++ event. Doing this requires a special Script
EventListener class that finds the event type guid with the Lua callback function.
We also need a place to store these listener objects, so a ScriptEventListenerMgr
classiscreatedaswell.First,we
'
'
ll look at the ScriptEventListener class.
class ScriptEventListener
{
EventType m_eventType;
LuaPlus::LuaObject m_scriptCallbackFunction;
public:
explicit ScriptEventListener(const EventType& eventType,
const LuaPlus::LuaObject& scriptCallbackFunction);
ScriptEventListener(void);
EventListenerDelegate GetDelegate(void)
{
˜
return MakeDelegate(this, &ScriptEventListener::ScriptEventDelegate);
}
void ScriptEventDelegate(IEventDataPtr pEventPtr);
};
The first member is the event type guid, and the second member is the Lua function
that will act as the listener delegate. These are both set in the
constructor. ScriptEventDelegate() is the true C++ listener delegate that acts
as the proxy to the Lua delegate.
void ScriptEventListener::ScriptEventDelegate(IEventDataPtr pEvent)
{
Search WWH ::




Custom Search