Game Development Reference
In-Depth Information
protected virtual functions
VBuildEventData()
and
VBuildEventFromScript()
.
All subclasses of this event should implement one or both of these functions.
VBuildEventData()
must be overridden if you want to fire this event from C++ and
have it be received by Lua. The
VBuildEventFromScript()
must be overridden by
events that are sent from Lua and received by C++. If you want both, then both func-
tions must be overridden.
These two functions perform the translation between C++ and Lua. Inside
VBuild
EventData()
, you are expected to fill out the
m_eventData
member with any
data you want passed to Lua. Inside
VBuildEventFromScript()
, you do the
opposite. You read the
m_eventData
member and fill out any C++ members you
want. Although you could just read the table when the event is received, it
s better
to do it here because
VBuildEventFromScript()
is only called once. The perfor-
mance costs are the same, regardless of how many receivers listen for the event.
The
RegisterEventTypeWithScript()
function registers the event type guid
with Lua. This guid maps the
ScriptEvent
subclass name to that guid. It does
this by adding to a global
EventType
table in Lua. This ensures that C++ and Lua
can refer to the same event using the same identifier.
'
void ScriptEvent::RegisterEventTypeWithScript(const char* key, EventType type)
{
// get or create the EventType table
LuaPlus::LuaObject eventTypeTable =
LuaStateManager::Get()->GetGlobalVars().GetByName(
“
EventType
”
);
if (eventTypeTable.IsNil())
eventTypeTable =
LuaStateManager::Get()->GetGlobalVars().CreateTable(
“
EventType
”
);
// error checking
GCC_ASSERT(eventTypeTable.IsTable());
GCC_ASSERT(eventTypeTable[key].IsNil());
// add the entry
eventTypeTable.SetNumber(key, (double)type);
}
First, this function gets or creates the
EventType
table and then it does some simple
error checking. After that, it assigns the guid to the table. Since this function is called
from the
REGISTER_SCRIPT_EVENT()
macro, it
'
s able to turn the
ScriptProcess
subclass into a string and use that as the key.
The
AddCreationFunction()
function is trivial, as it just inserts the
EventType
/
function pair
into the
static map. This
is
called automatically by
the