BGE Object List within a specified distance In a script that I'm writing I'm trying to create a list of all the objects within a specific distance of the player which have a certain property assigned to them, but I can't figure out how to get the script to search within that specified distance instead of across the entire scene.

For instance, to create a list of all the objects within a scene that contain a specific variable, I can use the code:

object_list = [ob for ob in scene.objects if ob.get("property_name")]


I have several hundred objects that contain this property, but I only want the objects that are within a specific distance from the player to be put into the list.

Now here's the dilemna: I need to create the list every frame, and I know it's possible to do what I want by scanning for every object in the scene with the property, but with several hundred (potentially thousands) of objects in the scene using this method becomes way too resource intensive.

So my question to you; Is it possible to only scan the objects within a specific distance from the player, for any object with the specified property?

To clarify, for ob in scene.objects scans the entire scene for a specific object. Is it possible to do exactly the same, but only for a small section of the scene (within a distance from the player)?
You can do it this way.


Add near sensor.





Get the list of hit objects.


Script if near sensor look for specific property name

from bge import logic

nearSen = logic.getCurrentController().sensors['Near']
list = nearSen.hitObjectList 


or if near sensor not look for specific property name

list = [obj for obj in nearSen.hitObjectList if nearSen.positive and 'property' in obj.getPropertyNames()]


Edit

The objects need to be set to actor so the sensor can detect them.



Download the blend to see how it works.


KX_NearSensor

KX_TouchSensor