Game Development Reference
In-Depth Information
then notified once the event occurs. So the drive to the amusement park is serene,
and once you arrive you simply notify all the kids that you've arrived.
An input system can be designed to be event based as well. There can be an over-
archingsystemthatacceptsregistrationstoparticularinputevents.Whentheevent
occurs, the system can then notify any code that registered to said event. So all
the systems that must know about the spacebar can register to a spacebar “just
pressed” event, and they will all be notified once it occurs.
Note that the underlying code for an event-based input system still must use a
polling mechanism. Just like how you as the driver must continue to check wheth-
er or not you've arrived at the park so you can notify the kids, the event-based
input system must be polling for the spacebar so it can notify the appropriate sys-
tems. However, the difference is that the polling is now being performed in only
one location in code.
A Basic Event System
Certain languages, such as C#, have native support for events. However, even if
events are not natively supported by your language of choice, it is very much pos-
sible to implement an event system. Nearly every language has support for saving
a function in a variable, whether it's through an anonymous function, lambda ex-
pression, function object, or function pointer. As long as functions can be saved to
variables, it is possible to have a list of functions that are registered to a particular
event. When that event is triggered, those registered functions can then each be
executed.
Suppose you want to implement a simple event-driven system for mouse clicks.
Different parts of the game code will want to be notified when a mouse click
occurs and in which location on screen. In this case, there needs to be a central
“mouse manager” that allows interested parties to register to the mouse click
event. The declaration for this class might look something like this:
Click here to view code image
class MouseManager
List functions
// Accepts functions passed as parameters with
signature of (int, int)
function RegisterToMouseClick( function hand-
ler ( int , int ))
Search WWH ::




Custom Search