2
$\begingroup$

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

$\endgroup$
  • $\begingroup$ I really hope this question to get answered, I don't want to list all the AI separately :S. $\endgroup$ – blackhole Jul 15 '14 at 4:52
  • $\begingroup$ The link to your demo is dead. $\endgroup$ – X-27 Mar 8 '15 at 17:27
  • 1
    $\begingroup$ DEAD LINK ALERT!!!!! PASTEALL - DESTROY!!! $\endgroup$ – X-27 Dec 28 '16 at 18:45
2
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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 $\endgroup$ – David Oct 24 '14 at 14:42
  • 1
    $\begingroup$ @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. $\endgroup$ – Goran Milovanovic Oct 24 '14 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.