Hi there.
I’ve run into a small problem with a project I’m working on. I have a CollisionTraverser called cTrav, and I wanted to pass it into an instance for something else. The relevant code looks like this:
def __init__(self):
self.cTrav = CollisionTraverser()
self.cTrav.traverse(self.render)
self.pusher = CollisionHandlerPusher()
self.pusher.addCollider(self.Spaceship.collisionNode, self.Spaceship.modelNode)
self.cTrav.addCollider(self.Spaceship.collisionNode, self.pusher)
def SetupScene(self):
self.Spaceship = player.Spaceship(self.loader, self.taskMgr, self.accept, self.cTrav, self.camera,"./Assets/Spaceship/Dumbledore.egg", self.render, "Spaceship", "./Assets/Spaceship/spacejet_C.png", (800, 800, 0), 75)
However, when attempting to use the CollisionTraverser in the spaceship class, it gives an AttributeError with the message “‘int’ object has no attribute ‘addCollider’”
The full code is here on my GitHub if it’s easier to look at. It tells the full picture a little better: GitHub - ToransuShoujo/Space-Jam
Thanks in advance!
Looking at your full code, it looks like the problem is that you’re calling “SetupScene” before you create your traverser.
As a result, “cTrav” (which is a variable defined in ShowBase, I believe) still has its default value–which is “0”. Hence being an “int”.
If I’m not much mistaken, the solution should be simple: create your traverser after the call to ShowBase’s “__init__” method, but before your call to “SetupScene”.
(By the way, if I may ask, why are you calling “traverse” so early in the code?)
Thanks so much, I’ll give this a shot!
I’ll be honest, I don’t really understand why we call traverse so early. This is a part of a class project so a lot of it is following instructions. We just happen to call it early in the code, though that isn’t how I would do it.
1 Like
To the former, it’s my pleasure! I hope that it helps! 
To the latter, ahh, fair enough! It might be worth asking your teacher what the reason is for that call to “traverse”: if there’s a good reason, then you may learn something that you might have missed; if there isn’t, then maybe the code will be improved–and you’ll likely still learn something!
(Depending on how open your teacher is to such questions, of course.)