array list problems

hi all, hopefully i get help here for my noob question! :smiley:

im doing two array lists, one for the enemys and one for my weapon (bullets). but, why just my last bullets are reacting on my enemy if im doing endless firing?


self.enemys = [None for i in range(count)]

    for i in range(count):
                    
        self.enemys[i] = loader.loadModel("models/enemy") 
        self.enemys[i].reparentTo(render)
        self.enemys[i].setScale(.2,.2,.2)
        self.enemys[i].setPos(enemyPOS(i))

def fire(self, task):

        if (self.keyMap["d"]!=0):
            
            
            
            self.proj = [None for ii in range(countX)]
            self.proj1 = [None for ii in range(countX)]
            
            for ii in range(countX):
                

                self.proj[ii] = loader.loadModel("projectil")      
                self.proj[ii].reparentTo(render)
                self.proj[ii].setScale(.2,.2,.2)

                self.proj1[ii] = loader.loadModel("projectil")      
                self.proj1[ii].reparentTo(render)
                self.proj1[ii].setScale(.2,.2,.2)
               
            
            playerH = modelLOADER.player.getH()
            playerP = modelLOADER.player.getP()
            
        
 
            X =modelLOADER.player.getX()-.15
            Y =modelLOADER.player.getY()
            Z =modelLOADER.player.getZ()-0.6
                
            X1 =modelLOADER.player.getX()+.15
            Y1 =modelLOADER.player.getY()
            Z1 =modelLOADER.player.getZ()-0.6
            
            self.direction1 = modelLOADER.player.getHpr()
                
            self.proj[ii].setPos(X,Y,Z)                
            self.proj[ii].setHpr(self.direction1)
            
            self.proj1[ii].setPos(X1,Y1,Z1)
            self.proj1[ii].setHpr(self.direction1)
            
           
            
            fire = LerpPosInterval(self.proj[ii], 500,Point3(0,-100,0), other = self.proj[ii])
            fire.start()
            fire2 = LerpPosInterval(self.proj1[ii], 500,Point3(0,-100,0), other = self.proj1[ii])
            fire2.start()
            fire1 = LerpColorInterval(self.proj[ii], 30, Vec4(0,0,1,1), Vec4(1,.5,0,1))
            fire1.start()
            fire3 = LerpColorInterval(self.proj1[ii], 30, Vec4(0,0,1,1), Vec4(1,.5,0,1))
            fire3.start()
            
                

            
            def projINTERACTION (task):

                for i in range(count):
                    
                    
                    disPROJ = self.proj[ii].getPos() - self.enemys[i].getPos()
                    disPROJlength1 = disPROJ.length()
                    disPROJ.normalize()

                    if (disPROJlength1 < 1):
                        print "HIT"
                        self.enemys[i].hide()

                return Task.cont
            taskMgr.add(projINTERACTION,"projTask")
            
        return Task.cont

thx for help…

you know there is [ code ] [ / code ] tags?

I think your problem is some place with naming the task the same or naming your intervals the same (or not naming them at all in this case)
consider “projTask%”%id(some object)
i cant test your code because its incomplete.

self.enemys = [None for i in range(count)]
for i in range(count):
    self.enemys[i] = loader.loadModel("models/enemy") 
    self.enemys[i].reparentTo(render)
    self.enemys[i].setScale(.2,.2,.2)
    self.enemys[i].setPos(enemyPOS(i))



def fire(self, task):
    if (self.keyMap["d"]!=0):     # we say if self.keyMap["d"]:
        
        self.proj = [None for ii in range(countX)]   #why 2 arrays?
        self.proj1 = [None for ii in range(countX)] 
        for ii in range(countX):
            self.proj[ii] = loader.loadModel("projectil")      
            self.proj[ii].reparentTo(render)
            self.proj[ii].setScale(.2,.2,.2)
            self.proj1[ii] = loader.loadModel("projectil")      
            self.proj1[ii].reparentTo(render)
            self.proj1[ii].setScale(.2,.2,.2)

        playerH = modelLOADER.player.getH()
        playerP = modelLOADER.player.getP()
        X =modelLOADER.player.getX()-.15
        Y =modelLOADER.player.getY()
        Z =modelLOADER.player.getZ()-0.6
        X1 =modelLOADER.player.getX()+.15
        Y1 =modelLOADER.player.getY()
        Z1 =modelLOADER.player.getZ()-0.6
        
        self.direction1 = modelLOADER.player.getHpr()
            
        self.proj[ii].setPos(X,Y,Z)                
        self.proj[ii].setHpr(self.direction1)
        
        self.proj1[ii].setPos(X1,Y1,Z1)
        self.proj1[ii].setHpr(self.direction1)
        
        fire = LerpPosInterval(self.proj[ii], 500,Point3(0,-100,0), other = self.proj[ii])
        fire.start()
        fire2 = LerpPosInterval(self.proj1[ii], 500,Point3(0,-100,0), other = self.proj1[ii])
        fire2.start()

        fire1 = LerpColorInterval(self.proj[ii], 30, Vec4(0,0,1,1), Vec4(1,.5,0,1))
        fire1.start()
        fire3 = LerpColorInterval(self.proj1[ii], 30, Vec4(0,0,1,1), Vec4(1,.5,0,1))
        fire3.start()

        
        def projINTERACTION (task):

            for i in range(count):
                
                disPROJ = self.proj[ii].getPos() - self.enemys[i].getPos()
                disPROJlength1 = disPROJ.length()

                # disPROJ.normalize() this statement does not make sence

                if (disPROJlength1 > 1):
                    print "HIT"
                    self.enemys[i].hide()

            return Task.cont
        taskMgr.add(projINTERACTION,"projTask")
    return Task.cont

wow, thx for the quick reply :slight_smile: but its still not working…
so here a quick demo which should shows you the array bug …

#test array list bug
#drh 2008

import direct.directbase.DirectStart
from direct.showbase import DirectObject
from direct.actor.Actor import Actor
from pandac.PandaModules import Vec3,Vec4,BitMask32
from direct.interval.IntervalGlobal import *
from direct.task.Task import Task
from pandac.PandaModules import Point3
import math

count = 100
raw = 10


class TEST(DirectObject.DirectObject):
    def __init__(self):
        
        base.disableMouse() 
        
       
        
        base.setBackgroundColor(0, 0, 0)
        self.keyMap = {"d":0,"h":0}


    # i changed to mouse controlls
        self.accept("d", self.Keys, ["d",1])
        self.accept("d-up", self.Keys, ["d",0])
    
        taskMgr.add(self.fire,"fireTask")

        self.accept("h", self.Keys, ["h",1])
        self.accept("h-up", self.Keys, ["h",0])
    
        taskMgr.add(self.ControllPlayer,"cpTask")

        def enemyPOS(i):
            return Point3((i%raw)-1, int(i/raw)-1 , 0)

        self.enemys = [None for i in range(count)]
             
        for i in range(count):
                
            self.enemys[i] = loader.loadModel("models/enemy") 
            self.enemys[i].reparentTo(render)
            self.enemys[i].setScale(.2,.2,.2)
            self.enemys[i].setPos(enemyPOS(i))
       
        self.player = Actor("player")
        self.player.reparentTo(render)
        self.player.setScale(.2,.2,.2)
        
        camPOSy = self.player.getY()+30
    
        base.camera.setPos(3.5,camPOSy,10)
        base.camera.setHpr(180,-20,0)
    
        self.startTIME = 1    
    #just for your tests- press s to rotate
    def ControllPlayer(self,task):
 

        
        if self.keyMap["h"]:
            XX = task.time - self.startTIME *33
            self.player.setHpr(10*XX,0,0)

        
        return Task.cont
    
    def Keys(self, key, value):
        self.keyMap[key] = value

    
            
            
    
    
    def fire(self, task):
    
                    
                if self.keyMap["d"]:
                    
                    
                    
                    self.proj = [None for ii in range(count)]
                    
                    
                    for ii in range(count):
                        
    
                        self.proj[ii] = loader.loadModel("projectil")      
                        self.proj[ii].reparentTo(render)
                        self.proj[ii].setScale(.2,.2,.2)
        
                    self.proj1 = loader.loadModel("projectil")      
                    self.proj1.reparentTo(render)
                    self.proj1.setScale(.2,.2,.2)
                       
                    
                    playerH = self.player.getH()
                    playerP = self.player.getP()
                    
                  
                        
                        
         
                    X =self.player.getX()-.15
                    Y =self.player.getY()
                    Z =self.player.getZ()-0.6
                        
                    X1 =self.player.getX()+.15
                    Y1 =self.player.getY()
                    Z1 =self.player.getZ()-0.6
                    
                    self.direction1 = self.player.getHpr()
                        
                    self.proj[ii].setPos(X,Y,Z)                
                    self.proj[ii].setHpr(self.direction1)
                    
                    self.proj1.setPos(X1,Y1,Z1)
                    self.proj1.setHpr(self.direction1)
                    
                   
                    
                    fireX = LerpPosInterval(self.proj[ii], 500,Point3(0,-100,0), other = self.proj[ii])
                    fireX.start()
                    fireX2 = LerpPosInterval(self.proj1, 500,Point3(0,-100,0), other = self.proj1)
                    fireX2.start()
                    fireX1 = LerpColorInterval(self.proj[ii], 30, Vec4(0,0,1,1), Vec4(1,.5,0,1))
                    fireX1.start()
                    fireX3 = LerpColorInterval(self.proj1, 30, Vec4(0,0,1,1), Vec4(1,.5,0,1))
                    fireX3.start()
                    
                        
        
                    
                    def projINTERACTION (task):
                 
                        
                        
                        for i in range(count):
                            
                            self.enemys[i].setScale(.2,.2,.2)
                            disPROJ = self.proj[ii].getPos() - self.enemys[i].getPos()
                            disPROJlength1 = disPROJ.length()

                  
                     
                            if (disPROJlength1 < 1):
                                print "HIT"
                                self.enemys[i].hide()
                            
                                
                   
                        return Task.cont
                    taskMgr.add(projINTERACTION,"ProjTask")
                    
                return Task.cont
X=TEST()        
run()

You don’t loop over ii in projINTERACTION. Actually ii seems not to be defined at all. This should give you an error…

the code tags!

i changed form the distance measuring to a collision recall…

thx for all the help :slight_smile:

# weapons drh 2008

import direct.directbase.DirectStart
from direct.showbase import DirectObject
from pandac.PandaModules import Vec3,Vec4,BitMask32
from direct.task.Task import Task
from direct.interval.IntervalGlobal import * 
from pandac.PandaModules import * 
import modelLOADER

count = 100
BULLETScount = 500

COLORintervalTIME = 30
cTrav1 = CollisionTraverser()

class WEAPON(DirectObject.DirectObject):
    def __init__(self):

        print "weapons loaded"
       
        
        base.setBackgroundColor(0, 0, 0)
        self.keyMap = {"d":0,"h":0}
    
        self.accept("mouse1", self.Keys, ["d",1])
        self.accept("mouse1-up", self.Keys, ["d",0])
    
        taskMgr.add(self.fire,"fireTask")

    def Keys(self, key, value):
        self.keyMap[key] = value

    
    def fire(self, task):
            
            
               
             

            for i in range(count):
                
                
  
                if self.keyMap["d"]:
                    
                    proj = [None for ii in range(count)] 
                    
                    proj[ii] = loader.loadModel("projectil")     
                    proj[ii].reparentTo(render)
                    proj[ii].setScale(.2,.2,.2)
                    
                    ###
                    ###COLLISION 
                    ###
                    modelLOADER.environ.setCollideMask(BitMask32.bit(0))
                    modelLOADER.enemys[i].setCollideMask(BitMask32.bit(1))
                                    
                    projENEMYRay = CollisionRay()
                    projENEMYRay.setOrigin(0,0,100)
                    projENEMYRay.setDirection(0,0,-1)
                                                            
                    projENEMYCol = CollisionNode('projRay')
                    projENEMYCol.addSolid(projENEMYRay)
                    projENEMYCol.setFromCollideMask(BitMask32.bit(1))
                    projENEMYCol.setIntoCollideMask(BitMask32.allOff())
    
                    projENEMYColNp = proj[ii].attachNewNode(projENEMYCol)
                    projENEMYQueue = CollisionHandlerQueue()
                    cTrav1.addCollider(projENEMYColNp, projENEMYQueue)

                    cTrav1.showCollisions(render)
                    projENEMYColNp.show()
                    
                    ###
                    
                    self.proj1 = loader.loadModel("projectil")      
                    self.proj1.reparentTo(render)
                    self.proj1.setScale(.2,.2,.2)
   
                    playerH = modelLOADER.player.getH()
                    playerP = modelLOADER.player.getP()

                    X =modelLOADER.player.getX()-.15
                    Y =modelLOADER.player.getY()
                    Z =modelLOADER.player.getZ()-0.6
                        
                    X1 =modelLOADER.player.getX()+.15
                    Y1 =modelLOADER.player.getY()
                    Z1 =modelLOADER.player.getZ()-0.6
                    
                    self.direction1 = modelLOADER.player.getHpr()
                        
                    proj[ii].setPos(X,Y,Z)                
                    proj[ii].setHpr(self.direction1)
                    
                    self.proj1.setPos(X1,Y1,Z1)
                    self.proj1.setHpr(self.direction1)
            
                    fireX = LerpPosInterval(proj[ii], BULLETScount,Point3(0,-100,0), other = proj[ii])
                    fireX.start()
                    fireX2 = LerpPosInterval(self.proj1, BULLETScount,Point3(0,-100,0), other = self.proj1)
                    fireX2.start()
                    fireX1 = LerpColorInterval(proj[ii], COLORintervalTIME, Vec4(0,0,1,1), Vec4(1,.5,0,1))
                    fireX1.start()
                    fireX3 = LerpColorInterval(self.proj1, COLORintervalTIME, Vec4(0,0,1,1), Vec4(1,.5,0,1))
                    fireX3.start()
                    
                    def COLLISION(task):
                        for i in range(projENEMYQueue.getNumEntries()): 
                            entry = projENEMYQueue.getEntry(i) 
                            point = entry.getSurfacePoint(entry.getIntoNodePath())
                            #point1 = entry.getSurfacePoint(getFromNodePath())
                            
                            
                            
                            #modelLOADER.enemys[i].remove()
 
                            print entry
                            print point
                            #print point1
                        
                        return Task.cont
                    taskMgr.add(COLLISION,"collisionTask")  
  
                return Task.cont

we = WEAPON()