Panda3D Manual: Event HandlersEvents occur either when the user does something (such as clicking a mouse or pressing a key) or when sent by the script using
Defining a class that can Handle EventsThe first step is to import class DirectObject: from direct.showbase import DirectObject With DirectObject loaded, it is possible to create a subclass of DirectObject. This allows the class to inherit the messaging API and thus listen for events. class myClassName(DirectObject.DirectObject): The sample below creates a class that can listen for events. The "accept" function notifies panda that the printHello method is an event handler for the mouse1 event. The "accept" function and the various event names will be explained in detail later. class Hello(DirectObject.DirectObject): Event Handling FunctionsEvents first go to a mechanism built into panda called the "Messenger." The messenger may accept or ignore events that it receives. If it accepts an event, then an event handler will be called. If ignored, then no handler will be called. An object may accept an event an infinite number of times or accept it only once. If checking for an accept within the object listening to it, it should be prefixed with self. If the accept command occurs outside the class, then the variable the class is associated with should be used. myDirectObject.accept('Event Name',myDirectObjectMethod) Specific events may be ignored, so that no message is sent. Also, all events coming from an object may be ignored. myDirectObject.ignore('Event Name') Finally, there are some useful utility functions for debugging. The messenger typically does not print out when every event occurs. Toggling verbose mode will make the messenger print every event it receives. Toggling it again will revert it to the default. A number of methods exist for checking to see what object is checking for what event, but the print method will show who is accepting each event. Also, if accepts keep changing to the point where it is too confusing, the clear method will start the messenger over with a clear dictionary. messenger.toggleVerbose() Sending Custom EventsCustom events can be sent by the script using the code messenger.send('Event Name') A list of parameters can optionally be sent to the event handler. Parameters defined in class Test(DirectObject): A Note on Object ManagementWhen a DirectObject accepts an event, the messenger retains a reference to that DirectObject. To ensure that objects that are no longer needed are properly disposed of, they must ignore any messages they are accepting. For example, the following code may not do what you expect: import direct.directbase.DirectStart Try the example above, and you'll find that the missiles fire even though the object that would handle the event had been deleted. One solution (patterned after other parts of the Panda3d architecture) is to define a "destroy" method for any custom classes you create, which calls "ignoreAll" to unregister from the event-handler system. import direct.directbase.DirectStart © Carnegie Mellon University 2010 |