Game Development Reference
In-Depth Information
In the application initialization, the REGISTER_SCRIPT_EVENT() macro must be
called:
REGISTER_SCRIPT_EVENT(EvtData_ScriptEventTest_ToLua,
EvtData_ScriptEventTest_ToLua::sk_EventType);
REGISTER_SCRIPT_EVENT(EvtData_ScriptEventTest_FromLua,
EvtData_ScriptEventTest_FromLua::sk_EventType);
Now the events are ready for Lua:
function TestEventHandler(eventData)
print("Event Received in Lua:
.. eventData)
eventData = eventData + 1
QueueEvent(EventType.EvtData_ScriptEventTest_FromLua, eventData)
end
RegisterEventListener(EventType.EvtData_ScriptEventTest_ToLua,TestEventHandler)
This code creates a listener function and registers it to listen for an event. The lis-
tener adds a number and then queues a new event, which is received by C++. The
event chain can be tested by firing off the C++ event.
shared_ptr<EvtData_ScriptEventTest_ToLua> pEvent(
GCC_NEW EvtData_ScriptEventTest_ToLua);
IEventManager::Get()->VQueueEvent(pEvent);
'
That will send an event from C++ that
s received by Lua, which in turn will send an
event from Lua that is received by C++.
Using events is a great way to communicate between C++ and Lua. It keeps Lua and
C++ nicely decoupled by ensuring that the listener doesn
t care about the source of
the event. That means you can move events freely from Lua to C++ or vice versa
without having to change any of the code on the listeners.
'
Script Component
So far, we
ve created a way to deal with script processing over multiple frames as well
as communicating between C++ and Lua using events. Another crucial piece to this
puzzle is the ability to manipulate actors through Lua. It wouldn ' t be a good idea to
start exposing tons of different components since that can get really messy, so instead,
a new type of component is created. This component knows how to access the other
components on the actor and call whatever functions are appropriate to expose.
The implementation of the script component is fairly trivial compared to everything
you
'
'
ve seen so far. Here
'
s the class declaration:
 
 
Search WWH ::




Custom Search