In addition to tasks, we have one more system in Panda3D that we can use to control dynamic elements in our game: events. In this chapter, we're going to use events and tasks to set up our cycle so that the player can move it around the track. We're also going to set up some important rules for our cycle's movement, such as acceleration and simulating drift.
This is what we'll talk about:
The event system is vital to making our game respond to anything other than the passage of time. It's not too complicated, but we still need to pay attention and make sure we don't miss anything!
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 this works.