Events and Inputs :: GameDev Panda3D cookbook

Inputs and events are common elements in software development as a whole. A user input is usually some interaction method given to the user be able to command the software. It can be triggered by a key, a mouse click, a voice detection system, a touch on the screen.
Events are commands exchanged between elements of software. In the case of a game, we can imagine the following situation: A character is walking through a castle, where there is a trigger of a trap. When the character goes through the trigger, the trigger signals the trap to shoot arrows that will kill the character.
In this short description, we can say that the trigger sent a message to the trap shoot. This message is what we call events.

Both user inputs and events between objects have a vital role in game development.

Let’s see how to catch a keyboard event, in this case, when the ‘W’ key was pressed.

sample 1:

User Inputs in Panda3D, are sent to the system as events. When a key is pressed, an event message is sent throughout the system, what developer must do, is set an object to “listen” to a specific event.

This procedure is done in two steps:
1- Set the event which we are interested in: self.accept( ‘event name’, function)
2- Define a function to deal with the event. Basically, “what must be done, after the event were triggered”.

In the sample above, the event created by the pressure in ‘W’ key, is caught and treated by the function ‘catchKeyPressed’.

sample 2:

In this second sample, we have new possibilities: parameters, and event ‘up’.

The event ‘-up’, is used to catch the event generated when a key is unpressed.


Events, like said before, are a way to make game object to talk with each other.
In general, there are two steps to make it happens:
1- Define which event an object needs to be aware of;
2- Give the commands to send the messages.

In Panda3D, user inputs are events, so we can catch events in the same way we configure user inputs.
Now, let’s see how to send some event.

sample 1:

When the ‘w’ key is pressed, a task is created, and the taskFunction is gonna make the cube move towards right. As soon as the cube x position be bigger than 3, an event is sent to the system (messenger.send( “stopCuboEvent”) ). This event is caught by our class and executes the stopCubeFunction, which deletes the task, and the cube stops.

Leave a Reply

Your email address will not be published. Required fields are marked *