delete all objects with a certain property

I have objects that get created all the time. all the duplicates have the same name “y line draw”. they all have the same boolean property “active”. I need a way of deleting all the duplicates that have their property “active” set as “True” if a given event occurs. here is what I’ve tried:


    a = 0

    for i in scene.objects:
        
        if i == "y line draw":

            a += 1                     #pretend this part deletes the objects
            print (a)


    a = 0
    obj  = scene.objects["y line draw"]

    for obj in scene.objects:
        
        a += 1
        print (a)                          #pretend this part deletes the objects

I know of a way I could do it in the logic editor, but I like to do as much as possible inside the code.

To iterate through objects based on names, you can use one of the Python string commands, and then get if the property is true.


if str(i) startswith(myObjectName):
         if i['Active'] == True:
                 i.endObject()

i dont know why but i get a syntax error when I try to use that startswith method, i may not know how to use it correctly.

If it doesnt work for you heres another way that will work for objects that are duplicates and have the same name although its probably slower.


i = [obj for obj in scene.objects if "y line draw" in obj.name and "active" in obj and obj["active"] == True]
for i in scene.objects:
    i.endObject()


how about that:


from bge.logic import getCurrentScene
...

def endObjectsWithPropertySet(objectName, propertyName):
    for object in getCurrentScene().objects:
        if (object.name == objectName and
           object.get(propertyName)):
            object.endObject()

monsters code worked for me. thank you all for the help.