NodePath.getChild() issues?

Im trying to make it where i can fire an infinite amount of bullets. I had it where i could but the previous bullet stopped when a new one was fired. now i can fire one than another and the first one goes still but the second one doesn’t move. and then when i try to fire a 3rd it crashes. any ideas? heres the code

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
from pandac.PandaModules import WindowProperties
from pandac.PandaModules import CollisionTraverser, CollisionHandlerEvent
from pandac.PandaModules import CollisionNode, CollisionSphere
from direct.showbase.ShowBase import ShowBase 
import sys

#this class has colisions with pushers and regular colliders

class Game(ShowBase, object):
    speed = .1 # controls the speed of the ship
    FORWARD = Vec3(0, 2, 0) # sets direction and speed of forward direction
    STOP = Vec3(0, 0, 0)# sets the speed and direction of stop
    walk = STOP # makes walk = stop
    collision = False #True if somthing is colliding
    ammoCount = -1
    def __init__(self):
        base.disableMouse() # disables the default mouse camera controls
        base.accept( "escape" , sys.exit) # quits when escape is pressed
        self.collisionsHandler()#This should be before ANY node loading/setting
        self.addlight() # load the light
        self.loadShip() #calls up the function to load the ship
        taskMgr.add(self.updateShipPos, 'update position')
        taskMgr.add(self.mouseUpdate, 'update mouse')
    def collisionsHandler(self):
        # Initialize the traverser.
        base.cTrav = CollisionTraverser()
        # Initialize the handler.
        self.pusher = CollisionHandlerPusher()
        # Make a variable to store the unique collision string count.
        self.collCount = 0
    def addCollision(self, model):
        # Setup a collision solid for this model.
        pushColl = self.initCollisionSphere(model, .7, False)#List variable for pusher
        pushaColl = pushColl[0]#Non-list varible for the pusher 

        # Add this object to the traverser.
        # Accept the events sent by the collisions.
        self.accept('into-' + pushColl[1], self.collideIn)
        self.accept('outof-' + pushColl[1], self.collideOut)

        self.pusher.addCollider(pushaColl, model,
        base.cTrav.addCollider(pushaColl, self.pusher)


    def collideIn(self, collEntry):
        self.collision = True
    def collideOut(self, collEntry):
        print("D: OH SHIT WE DIED")
        self.collision = False
    def initCollisionSphere(self, obj, sizeMult, show=False):
        # Get the size of the object for the collision sphere.
        bounds = obj.getChild(0).getBounds()
        center = bounds.getCenter()
        radius = bounds.getRadius() * sizeMult
        # Create a collision sphere and name it something understandable.
        collSphereStr = 'CollisionHull' + str(self.collCount) + "_" + obj.getName()
        self.collCount += 1
        cNode = CollisionNode(collSphereStr)
        cNode.addSolid(CollisionSphere(center, radius))
        cNodepath = obj.attachNewNode(cNode)
        if show:
   the collision model
        print collSphereStr
        # Return a tuple with the collision node and its corrsponding string so
        # that the bitmask can be set.
        return (cNodepath, collSphereStr)
    def addlight(self): #create the light
        #create a directional light to give a sense of 3d ness
        dlight = DirectionalLight('dlight')
        dlight.setColor(VBase4(0.8, 0.8, 0.8, 1))
        dlnp = render.attachNewNode(dlight)
        dlnp.setHpr(0, -60, 0)
    def addWinProps(self):
        #defines the windows properties aka title and hide cursor etc
        props = WindowProperties()
        props.setTitle('Space Fighter')
    def loadShip(self): # just loads the ship model
        self.ship = loader.loadModel('ship')
        self.addCollision(self.ship)#adds a collision/pusher to the ship model
    def loadAI(self): = loader.loadModel('ai') (.5)
        self.addCollision( a collision/pusher to the ai model
    def shoot(self):
        self.accept('mouse1', self.loadAmmo)
        self.accept('mouse3', self.loadAmmo)
    def loadAmmo(self):
        self.ammo = loader.loadModel('bullet')
        self.ammoCount += 1
        ammoH = self.ship.getH()
        ammoP = self.ship.getP()
        ammoR = self.ship.getR()
        self.ammo.setHpr(self.ammo, ammoH, ammoP, ammoR)
        taskMgr.add(self.updateAmmoPos, 'MyTaskName', extraArgs=[self.ammo.getChild(self.ammoCount)], appendTask=True)   
    def loadWorld(self): # loads world = loader.loadModel('level'), 30, 3)
        #self.addCollision( a collision/pusher to the world model. commented out            cause 'you dont need world collisions' :P
    def setCamera(self): # sets the camera lense
        place =
    def mouseUpdate(self,task): # updates the mouse and changes ships P and H accordingly
        md =
        x = md.getX()
        y = md.getY()
            self.ship.setP(self.ship.getP() -  (y -*0.1)
            self.ship.setH(self.ship.getH() - (x -*0.1)
        return task.cont
    def createControls(self): # changes the value of walk to either STOP or Forward depending on the keypress
        base.accept( "w" , self.__setattr__,["walk",self.FORWARD])
        base.accept( "w-up" , self.__setattr__,["walk",self.STOP] )
    def updateShipPos(self,task): # task to move the ship and camera
        return task.cont
    def updateAmmoPos(self, ammo ,task):
        print ammo

        return task.cont

Might be useful to tell us more about how the crash happened, show the error that was displayed, etc.

Your loadAmmo() method replaces the ammo model with a new one each time. The code crashes after three bullets because you load a fresh model and call getChild(2) on it, and it doesn’t have a third child.

I think if you just do:

taskMgr.add(self.updateAmmoPos, ‘MyTaskName’, extraArgs=[self.ammo], appendTask=True)

your code will basically work, but you probably should rethink the way you create bullets. Instancing would be a really good idea here, since you might have a lot of bullets being rendered at once.

yea i used some getChild() magic and got it to work :smiley:. took a while tho lol. Thanks for the tips :smiley:

def collideIn(self, collEntry):
self.collision = True "

Next time , as enticing as that lovely code was, could you leave out your filthy languagee please ? :wink:

ttttthank you :wink:

:open_mouth: i didnt even realize that lol, my apologies XD i dont usually expect my code to be read by others XD