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.

Tasks :: Gamed Panda3D cookbook

“Tasks” is a function used in Panda3D, which refer to: “The need to provide for certain game objects, execution loops.”
For example, when creating a character in a game, the programming of this character may have some functions that should be checked, and executed in every single frame.
Different game engines have different names, and ways to implement these procedures. Here we’ll see how it works in Panda3D.

Panda3D —————————–

sample 1:

To build a Task in Pand3D is very simple.
In line: self.taskMgr.add(self.taskFunction, “testing Task”), the task named “testing Task”, is created. And the function self.taskFunction is set as the function which is going to be called every frame.

It is free for the developer, choose the method he wants to set for the tasks, as well, decide the functions names as his wish.

The task function, in the sample above, has a mandatory parameter, called “task” and a return value, also required, which is a Task constant, in case, Task.cont.

sample 2:

In this last sample code, it is possible to see, how to modify a game object positioning properties (It worth mention that basically, any property might be edited during a task.) In this case, the x value is changed in 0.01, every single task execution, giving the illusion that the cube is moving towards the right.

Is possible to create some more specific behaviors in tasks, like, stopping the task execution after a while:

sample 3:

In this sample, the return value for the task function will be Task.cont (“Task.continue”), while the cube x position is less than 3. As soon the cube x position, become bigger than 3, the return value is gonna be Task.done, and the task will no longer be executed.

sample 4:

In this last sample, it is possible to see that the cube moves in intervals of two seconds. In this sample, we use the method ‘doMethodLater’, that allows the developer to set a time wait, till the task start be executed. And when the task is executed, a delay time (task.delayTime) is set to retard the next task execution.

Tasks run a major role in Panda3D games and are use in many situations. So… master them.