Let's add a PythonTag
to our CollisionNodes
so we can print out the names of the cycles when they collide.
setupCollisions
method of our Cycle
class, add the following line of code right after we create self.shieldCN:
self.shieldCN.setPythonTag("owner", self)
bump
method, replace the print
statement we have with the following:print(entry.getFromNodePath().getPythonTag("owner").name) print("has bumped into:") print(entry.getIntoNodePath().getPythonTag("owner").name) print("")
CycleClass_04.py
. WorldClass_03.py
to import CycleClass_04.py
instead of CycleClass_03.py
. Then, save it as WorldClass_04.py
and run it from the command prompt. Make sure to watch the command prompt when the cycles collide and you see something similar to the following screenshot:When we bump our cycles together, we see the output in the command prompt that tells us who is bumping into whom. That's because we are getting the CollisionNodes
from the collision entry, and then getting the class instances from the PythonTags
, and finally we get the names from the class instance. Easy as pie!
Hop back over to that simple collision system we made at the beginning of the chapter. Add a couple more collision spheres to it and give them PythonTags
with strings in them, and different BitMasks
. Use CollisionHandlerPushers
or CollisionHandlerEvents
and play around with BitMasks
and PythonTags
until you feel comfortable with how they work. These concepts are important to using collision detection so we have to make sure we understand them.
PythonTags
are a very useful utility available to us, both for collision detection systems and other applications as well. Let's take a moment to test our understanding of how to use them.
PythonTag?
PythonTag?
PythonTag?
PythonTags
that we must be careful of?