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.addWinProps()
        self.addlight() # load the light
        self.loadShip() #calls up the function to load the ship
        self.loadAI()
        self.loadWorld()
        self.createControls()
        self.setCamera()
        self.shoot()
        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()
        self.pusher.addInPattern('into-%in')
        self.pusher.addOutPattern('outof-%in')
        
        # 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.drive.node())
        base.cTrav.addCollider(pushaColl, self.pusher)

        print(pushColl[1])

    def collideIn(self, collEntry):
        print("FUUUUUUU- FUCKING EXPLOSION")
        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:
            cNodepath.show()#Shows 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))
        dlight.setShadowCaster(True)
        dlnp = render.attachNewNode(dlight)
        dlnp.setHpr(0, -60, 0)
        render.setLight(dlnp)
        render.setShaderAuto()
        
    def addWinProps(self):
        #defines the windows properties aka title and hide cursor etc
        props = WindowProperties()
        props.setCursorHidden(True)
        props.setTitle('Space Fighter')
        base.win.requestProperties(props)
        
    def loadShip(self): # just loads the ship model
        self.ship = loader.loadModel('ship')
        self.ship.reparentTo(render)
        self.ship.setTwoSided(True)
        self.ship.setScale(.5)
        self.addCollision(self.ship)#adds a collision/pusher to the ship model
        
    def loadAI(self):
        self.ai = loader.loadModel('ai')
        self.ai.reparentTo(render)
        self.ai.setTwoSided(True)
        self.ai.setScale (.5)
        self.addCollision(self.ai)#adds 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.ammo.setScale(.2)
        self.ammo.reparentTo(render)
        self.ammo.setPos(self.ship.getPos())
        
        self.ammoCount += 1
        
        ammoH = self.ship.getH()
        ammoP = self.ship.getP()
        ammoR = self.ship.getR()
        self.ammo.setHpr(self.ammo, ammoH, ammoP, ammoR)
        
        self.addCollision(self.ammo)
        
        taskMgr.add(self.updateAmmoPos, 'MyTaskName', extraArgs=[self.ammo.getChild(self.ammoCount)], appendTask=True)   
        
    def loadWorld(self): # loads world
        self.world = loader.loadModel('level')
        self.world.reparentTo(render)
        self.world.setPos(-1, 30, 3)
        self.world.setScale(80)
        #self.addCollision(self.world)#adds a collision/pusher to the world model. commented out            cause 'you dont need world collisions' :P
        
    def setCamera(self): # sets the camera lense
        place = base.cam.node().getLens()
        place.setFov(70)
        base.cam.node().setLens(place)
        base.camera.reparentTo(self.ship)
        base.camera.setY(-30)
        base.camera.setZ(5)
        
    def mouseUpdate(self,task): # updates the mouse and changes ships P and H accordingly
        md = base.win.getPointer(0)
        x = md.getX()
        y = md.getY()
        if base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2):
            self.ship.setP(self.ship.getP() -  (y - base.win.getYSize()/2)*0.1)
            self.ship.setH(self.ship.getH() - (x - base.win.getXSize()/2)*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
        self.ship.setPos(self.ship,self.walk*self.speed)
        return task.cont
        
    def updateAmmoPos(self, ammo ,task):
        ammo.setPos(ammo,self.FORWARD*self.speed)
        print ammo
        

        return task.cont
        
Game()
run()

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):
print(“FUUUUUUU- FUCKING EXPLOSION”)
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