Detecting objects with a property nearest to the player

I need to figure out what the closest object with a certain property to the player is, what’s the best way to do this? In Python, not with near sensors

Well you could subtract the worldPosition of each object from the worldPosition of the player then compare the differences to work out which is smallest.

Simple solution:

enemy_list = [o for o in bge.logic.getCurrentScene().object if 'property' in o]
closest = [100000, None]

for e in enemy_list:
    if player.getDistanceTo(e) < closest[0]:
        closest = [player.getDistanceTo(e), e]

More complex solutions can be found in the resources forum. Ages ago I posted a whole bunch of functions, one was a
‘getBestToTrackTo’ that involved checking to see if walls were in the way, if I could actually see them etc.

thanks sdfgeoff, I don’t think I’ll need any of those though. This will work just fine, and I believe its what MrPutuLips suggested too so thanks