Events are basically messages that are handled by the messenger
object created by DirectStart
. When an event occurs, all of the objects that have been told to respond to that event will call a specified function. It's really a pretty simple system. Most of the set up of events is handled for us when the messenger
is created, which makes them very easy to use.
The first step to using events is to register an event response. To do this, we need to use the accept()
method. By default, our World
class doesn't have an accept()
method, so to give it one, we need to inherit that method from a built-in class called DirectObject
.
Once we have the accept()
method, we can use it like this:
self.accept([eventName], [function])
The event name we give to the accept()
method is a string that serves as an identifier for the unique event we want to respond to. The function we pass in will be called when the event occurs.
Let's set up a simple event-response system in our program to see how...