OO styles - Inherit NodePath

Which one is the most customary:

1 - An entity class with a NodePath attribute.
2 - An entity class inheriting NodePath.

My inclination would be option 2, but I can’t find example of this. Alternatively, I can cheat by forwarding some calls on an instance to its attribute model’s method so I can do this:


You can find an example of subclassing NodePath by looking at the Actor class, you can find it in the direct/src/actor/Actor.py file.

I do #1. I think i have a post explaining this though. It does come up often.
I use it mainly because NodePath is c++ while your class is in python. If you put your class in scene graph you will not be able to get your class out again just a nodepath …

Yeah #1 is best, with optionally forwarding some nodepath methods directly as you suggest. That’s what I do. I think in general in object-oriented programming you want to avoid creating big, complex inheritance hierarchies, therefore you should favor object composition (of which #1 is an example) over class inheritance except where class inheritance is really convenient.

Panda classes like nodepath are already part of panda’s own inheritance hierarchy so it seems best not to add to it. And panda does some odd tricks some times with C/Python that can trip you up if you subclass panda classes.

So go with #1. And if your nodepaths are colliding or something, and you get the nodepath object out of the collision system when you need to get your custom object that holds the nodepath, a good trick is to have your class tag its nodepath object with a reference to itself. See nodepath methods like setPythonTag, getPythonTag, etc.

The C++ wrapping business is definitively a good reason to go for Option 1!