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.
-
$\begingroup$ I really hope this question to get answered, I don't want to list all the AI separately :S. $\endgroup$– blackholeJul 15, 2014 at 4:52
-
$\begingroup$ The link to your demo is dead. $\endgroup$– X-27 is done with the networkMar 8, 2015 at 17:27
-
$\begingroup$ Almost 5 years later, and the link is still dead. #sad mess $\endgroup$– X-27 is done with the networkDec 31, 2019 at 6:30
1 Answer
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.
-
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$– DavidOct 24, 2014 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 whatcont.actuators
represents. $\endgroup$ Oct 24, 2014 at 18:07