Bullet Firing - Script runs only once per bullet

I have been having this problem for some time now, and I don’t know what is causing it. Basically, a script is called once by a Tap Always sensor, this appends a function to the pre_draw, therefore making it run every frame. See https://blenderartists.org/forum/showthread.php?438044-ccd-projectile-motion-manager

Anyway, what happens is that the script that should run every frame only runs once per bullet. Here is the script itself:

from bge import logic, render

scene = logic.getCurrentScene()
cont = logic.getCurrentController()


def main():
    own = cont.owner  
    own["Bullets"] = {}
    scene.pre_draw.append(BulletSystem) 
    #this runs once
     
def BulletSystem():
    #this runs every frame
    manager = cont.owner
    manager["debugList"] = str(manager["Bullets"])
    print(manager["debugList"])
    keys = []
    for key in manager["Bullets"]:        
        bullet = manager["Bullets"][key]
        if not bullet.invalid: 
            start = bullet.worldPosition          
            end = start + (bullet.worldLinearVelocity * 0.05)
            print("Start:", start, "End:", end)
            hitObj, _ , _ = bullet.rayCast(end,start,0,"",0,0,0)
            render.drawLine(start, end, [1, 0, 0])
            if hitObj:
                print("I hit" + hitObj.name)
                if hitObj.children:
                    for child in hitObj.children:
                        if "turret" in child:
                            child["destroyed"] = True
                scene.addObject("DustGen", bullet, 10.0)
                bullet.endObject()                             
            else:
                keys.append(key)
        else:
            keys.append(key)
                
        
    for keyz in keys:
        del manager["Bullets"][keyz]

The important part is that the BulletSystem() runs every frame. So then why does the for loop not?

Not sure, but probably due to defining of the owner?
the script can only be used by one owner, because you define/let it run once.

put cont and scene in the functions.


def test(cont):
    own = cont.owner
    scene = own.scene

Do this in every function that you need the owner.
you dont have to use the build in controller, you can use your own lines as well, but put it inside the function(s)


           if hitObj:
                print("I hit" + hitObj.name)
                if hitObj.children:
                    for child in hitObj.children:
                        if "turret" in child:
                            child["destroyed"] = True
                scene.addObject("DustGen", bullet, 10.0)
                bullet.endObject()                             
            else:
                #I think this is it
                keys.append(key)# this removes the bulllet if it hits nothing :D

@BluePrintRandom But I already have that part of the code. EDIT: Wait, you mean that that is the part that is wrong? @Cotaks, I will try that. I will let you guys know if I succeed (hopefully) or not.

EDIT: Nope, I changed those things Cotaks said, but nothing…

Good news! I solved it! All it took was removing 1 line…