get list in python

hi i am working on an tower defense game and i need to know how to get a list of all objects with a property then trake to that object till its a sertan distances thanks i know its vague if you need it clearer just ask

thanks mason

look for my tower defense template…

Hey mason,

here is a function which gets the closest object:

def GetClosest(target, prop):
    obj_list = [obj for obj in GameLogic.getCurrentScene().objects if obj.get(prop)]
    closest = obj_list[0]
    distance = target.getDistanceTo(closest)

    for object in obj_list[1:]:
        new_distance = target.getDistanceTo(object)
        if new_distance < distance:
            closest = object
            distance = new_distance

    return(closest)

Just call it like:

closest_object = GetClosest(tower, property)

The whole setup may look a bit like this:

#Tower.py
cont = GameLogic.getCurrentController
own = cont.owner

if not own.get('init'):
    own['target'] = None
    own['init'] = True

if own['target'] == None:
    closest = GetCloses(own, 'bad guy')
    if own.getDistanceTo(closest) < 10:
        own['target'] = closest
elif own.getDistanceTo(closest) < 10:
    vec = own.getVectTo(own['target'])
    own.alignAxisToVect(vec[1], 1, 1)
    #attack
else:
    own['target'] = None

thanks a lot guys andrew that was just what i needed thanks so much

mason