The next custom class we're going to make will be the Cycle
class, which will represent our characters, the hover cycles for us. Later on, when we start to have these characters interact with one another, we'll want to be able to use NodePath-type
operations such as setPos
on them. That means we need to add NodePath
functionality to the class.
Before we jump the gun and try to inherit from NodePath
when we make the class, like how the World
class inherits from DirectObject
, there's something we need to understand about Panda3D. In order to make Panda3D run faster, many of its classes are written in C++, NodePath
included among them. These C++ classes can't be directly inherited from by a class in Python. That crossover just doesn't work. Instead, we need to use a little workaround.
Instead of making our class itself a NodePath
, we're going to create a proxy NodePath
within our class to act as the root of the class instance. This NodePath
, which we'll call root
for convenience, will serve as the parent for all the other NodePath
in the class. We'll also use it to determine the exact position of the class instance in the world. We can even add setPos()
and getPos()
methods to our class that will call setPos()
and getPos()
on our root
so that the class behaves like we inherited from NodePath
.
To get the proxy NodePath
, we'll use a new method called attachNewNode()
that all NodePath
have, including render
. This method creates a new NodePath
that points to whatever node is passed to it, or if a string is passed in it creates a NodePath
named after the string that doesn't point to any nodes at all. The NodePath
is returned, so we need to place it into a variable to get a reference to it. For example:
self.root = render.attachNewNode("Root")
This statement will create a new NodePath
that is a child of render
, has the name Root
, and can be accessed through the self.root
variable.