Get near Objects by Property

I have a Player Character and Lots of Objects with Logic surrounding the Player (Enemies). Right now, it’s all those Enemies which execute their Scripts/Logic and I would like to see if I could successfully lower the Logic if it was in the Player Script (since the Enemies only behave when the Player is near). However, I don’t use the Near Sensor but make the Logic dependant on the Distance via getDistanceTo() and I would like to stick to that, as it is faster than the Near Sensor.
So, if I use rayCast or a Ray-, Near- or Radar Sensor, I would get the hitObject. My Question is, how can I get and define an Object or a List of Objects within the Player’s Distance that has the Property “x”?

if Player.getDistanceTo(somethingWithProperty(“x”)):
(And don’t tell me I should have put that in CODE Tags! :stuck_out_tongue: )

I guess or think that this Question has been asked before in either this or a similar Way, but neither the Search Function nor manual Searching helped me.

Right now my Logic is at ~15% with ~125 Enemies in the Level. Test-wisely commenting out the entire Enemy Script lowered the Logic Percentage to ~5% – that’s what I would like to get. :3

  1. Get a list of all objects in the scene with the required property. (I have a code snippet for that if you want)
  2. For every object in that list run a function like:
for posobj in objList:
    if player.getDistanceTo(posobj) > 70:
    do some other stuff here.....

You’ll probably not get the percentage that low - looping through the enemies may not help much. You could try it, though. You know, my BGHelper module has a Near function that allows you to get all objects near an object with a certain property, and can even present this list in nearest-furthest sorting.

Thank you both.
But if it wouldn’t make enough of a Difference, I’m probably better off not bothering – after all, I rather wouldn’t want to waste my Time in this Case. :confused:

However, I don’t use the Near Sensor but make the Logic dependant on the Distance via getDistanceTo()

Using the near sensor is not that bad though. With it you can “activate/deactivate” the enemies logic which helps a lot to decrease the fps. I use this method in Gtown for the arrow weapon.

Expensive method
Your problem is that all enemies are sensing all the time.
n enemies = n sensors

A cheap method
You might want to sense the other way around.
Let the player sense for enemies.
One player = one sensor.

Mixed method:
While the above method is cheap it is not very flexible as all enemies are treated the same assuming a spherical sensor area.
Much more flexibility is a “Behavior LOD”.

Let the player sense for the enemies as with the cheap method.
Notify all enemies that are sensed (message).
The sensed enemies can now switch on their own sensors (near, radar, Python) to look for the player. This allows specific enemy behavior (e.g. sensing the player when in front of the enemy only)

All enemies not sensed by the player can deactivate their sensors (= state switch) as the player is not near.

This method requires some knowledge in Python and states.

I have some Knowledge of Python, though I am not so much into handling States. (However, this even seems to be the easier Part about it.)

I have, by the Way, recognized that even with all my Enemies sensing all the Time and without reacting just when the Player gets close I can get a Logic of ~5% if I use a TrackTo- and a Motion Actuator instead of a Script, but the Behavior becomes much simpler and the Tracking does by far not work as well as with the scripted Method… wonder if that could be worth it. :confused: