Game Engine Architecture

(Ben Green) #1

776 14. Runtime Gameplay Foundation Systems


Some game engines call these things messages or commands instead of events.
These names emphasize the idea that informing objects about an event is es-
sentially equivalent to sending a message or command to those objects.
Practically speaking, event objects are usually not quite this simple. We
might implement diff erent types of events by deriving them from a root event
class, for example. The arguments might be implemented as a linked list or a
dynamically allocated array capable of containing arbitrary numbers of argu-
ments, and the arguments might be of various data types.
Encapsulating an event (or message) in an object has many benefi ts:
z Single event handler function. Because the event object encodes its type
internally, any number of diff erent event types can be represented by an
instance of a single class (or the root class of an inheritance hierarchy).
This means that we only need one virtual function to handle all types of
events (e.g., virtual void OnEvent(Event& event);).
z Persistence. Unlike a function call, whose arguments go out of scope
aft er the function returns, an event object stores both its type and its
arguments as data. An event object therefore has persistence. It can be
stored in a queue for handling at a later time, copied and broadcast to
multiple receivers, and so on.
z Blind event forwarding. An object can forward an event that it receives to
another object without having to “know” anything about the event. For
example, if a vehicle receives a Dismount event, it can forward it to all
of its passengers, thereby allowing them to dismount the vehicle, even
though the vehicle itself knows nothing about dismounting.
This idea of encapsulating an event/message/command in an object is com-
monplace in many fi elds of computer science. It is found not only in game
engines but in other systems like graphical user interfaces, distributed com-
munication systems, and many others. The well-known “Gang of Four” de-
sign patt erns book [17] calls this the Command patt ern.

14.7.3. Event Types
There are many ways to distinguish between diff erent types of events. One
simple approach in C or C++ is to defi ne a global enum that maps each event
type to a unique integer.
enum EventType
{
EVENT_TYPE_LEVEL_STARTED,
EVENT_TYPE_PLAYER_SPAWNED,
Free download pdf