Our Next Task
In which we learn about updating via tasks
Often, we want Panda to run some method repeatedly–as in the case of an “update” loop–or after a certain amount of time. For these purposes, Panda provides “tasks”.
Tasks are handled, appropriately, by a class named “TaskManager”. And once again, Panda provides a default, globally-accessible manager, in this case in a variable called “taskMgr”.
When we want to add a task to be run immediately, we have the task-manager “add” it, and when we want to run a task after a specified amount of time, we ask the task-manager to “doMethodLater”. We can also have a running task removed from the manager, if we no longer want it.
We’re going to use the task-manager to run an update loop, calling a method named “update”.
(We’re storing the update-task in case we want to do something with it later. We won’t in this case, but it’s perhaps not bad practice.)
Note that we return “Task.cont”–this is important, as it tells Panda that we want to run the task again. Without it, the task would run only once.
Now, let’s have those controls actually do something! Specifically, let’s move our character around a bit.
You may recall that in an earlier lesson we saw how to set the position of a NodePath. We’ll do the same here, setting our “tempActor” NodePath’s position to be a short distance from where it was at the start of the update.
But if we just move it by a constant value, variations in frame-rate will vary how fast it moves. So, we will access the time since the last update via another Panda global variable, the “globalClock”. We then multiply our movement by this value.
(I’ve also removed the print-statement from “updateKeyMap”, by the way, since we now have a more-visual means of seeing at least some of our key-pressed taking effect.)
Of course, right now we can just run through the walls. That won’t do…