778 14. Runtime Gameplay Foundation Systems
should not forget to account for the costs of sett ing up such a system, as they
are not insignifi cant.
14.7.4. Event Arguments
The arguments of an event usually act like the argument list of a function, pro-
viding information about the event that might be useful to the receiver. Event
arguments can be implemented in all sorts of ways.
We might derive a new type of Event class for each unique type of event. The
arguments can then be hard-coded as data members of the class. For example:
class ExplosionEvent : public Event
{
float m_damage;
Point m_center;
float m_radius;
};
Another approach is to store the event’s arguments as a collection of vari-
ants. A variant is a data object that is capable of holding more than one type of
data. It usually stores information about the data type that is currently being
stored, as well as the data itself. In an event system, we might want our argu-
ments to be integers, fl oating-point values, Booleans, or hashed string ids. So
in C or C++, we could defi ne a variant class that looks something like this:
struct Variant
{
enum Type
{
TYPE_INTEGER,
TYPE_FLOAT,
TYPE_BOOL,
TYPE_STRING_ID,
TYPE_COUNT // number of unique types
};
Type m_type;
union
{
I32 m_asInteger;
F32 m_asFloat;
bool m_asBool;
U32 m_asStringId;
};
};