# Blender game engine track to a property

I there any way to duplicate the action of the Edit object > Track to Actuator; so that it can track to a game property and not a object? It would be used to track multiple objects without having to list them all as separate track to actuators in the logic bricks.

blend file demo

• I really hope this question to get answered, I don't want to list all the AI separately :S. Jul 15, 2014 at 4:52
• Almost 5 years later, and the link is still dead. #sad mess Dec 31, 2019 at 6:30

You can only track one object at any given time, so you'll need to define a set of priorities by which a single target can be selected from a group of valid candidates (objects with the relevant property).

For example, if you wanted to track the closest object with the relevant property, you could do it with this function:

# track.py

def target_prop(cont):
self = cont.owner
objects = self.scene.objects

tracked = [o for o in objects if self["target_prop"] in o]
tracked.sort(key=lambda o: self.getDistanceTo(o))

target, *_ = tracked

act_track, *_ = cont.actuators
act_track.object = target

for act in cont.actuators:
cont.activate(act)


Example: track_to_prop.blend

As you can see, you can specify the target property via a property on the tracking object (target_prop in this case). Also, you can set the always sensor to "true level triggering" (aka: "pulse mode"), to constantly re-evaluate the closest target, if that's required.

• Will you add some info as to what *_ on lines 8 and 10 does? Also will to explain why you need to loop through the actuators to activate them. Thanks Oct 24, 2014 at 14:42
• @David That's just iterable unpacking syntax, to get the first element. The variable preceded by the star (the underscore in this case) is a list containing the rest of the elements, with act_track referencing the first. As for the actuators: You don't need to use a loop, as long as you can activate the relevant actuators, but I think a loop makes sense in this case, because I want to activate all that are connected, and that's what cont.actuators represents. Oct 24, 2014 at 18:07