track to object linked as asset

Hey all,
me again… sorry for bothering you so often.
I have a problem. I have an asset object, that is linked from another blendfile. I want this object to track to my main character.
To achieve this I have created an object that is called the same as my character. In the asset blend everthing works fine. But when I link this object to my scene, the track to doesn’t work, it seems that the track to doesn’t find the object it needs to track to.

Is it possible to create a track to that is linkable to other files?

I think you need to set the actuators object with a Python controller.

The actuator does not store the name of the target object but the reference to the target object. That means it does not matter if you have another object with the same name the actuator will not update itself. You need to do that.

You could make the logic to

  • read the targets name from a property or
  • a property name that the target needs from another property:

Setup.py:


import GameLogic
try:
  import Mathutils
  PREFIX = "OB"
except ImportError:
  PREFIX = ""
 
PtargetName = "targetName"
PtargetProp = "targetProp"
 
#--- BGE callable
def setTarget(cont):
  if not onePositive(cont):
    return
 
  own = cont.owner
  target = getTargetByName(own)
 
  if target is None:
    target = getTargetByProp(own)
 
  setActuatorsObject(cont, target)
 
#--- internal methods
def getTargetByName(own):
  targetName = own.get(PtargetName)
 
  if targetName is None:
    return None
 
  return GameLogic.getCurrentScene().objects[PREFIX + targetName]
 
def getTargetByProp(own):
  targetProp = own.get(PtargetProp)
 
  if targetProp is None:
    return None
 
  allObjects = GameLogic.getCurrentScene().objects
  objectsWithProp = [obj for obj in allObjects if targetProp in obj]
 
  if not objectsWithProp:
    return None
 
  return objectsWithProp[0]
 
def setActuatorsObject(cont, objValue):
  for actuator in cont.actuators:
    try:
      actuator.object = objValue
    except AttributeError:
      continue # actuator has no attribute object
 
    cont.activate(actuator)
 
def onePositive(cont):
  for sensor in cont.sensors:
    if sensor.positive:
      return True
  return False

always sensor (no pulses) -> Pythom controller Module Mode: Setup.setTarget -> trackto actuator

it is untested
I hope it helps

I also need to do the same thing and I do know nothing about python…
I need to track Cube.081 (that´s the first player) but I don´t know what things I should replace in that python script.
I have my blend file here
http://www.megaupload.com/?d=AFFZUQSE

Thanks!!!

You do not need to edit the code. [A good script shouldn’t need a change if used in the right context. Just the context might need a change]

What you need is:
copy&paste the above Python code in the blender text window. Give the text block the name Setup.py.

Now you have two options:
(Naming method)
tracking object with

  • a string property “targetName” with value “Cube.081” (Why this name?)
  • the SCA (sensor-controller-actuator) setting from post #2

or
(Property method)
tracking object with

  • a string property “targetProp” with value “target” (or whatever you like)
  • the SCA (sensor-controller-actuator) setting from post #2

at object Cube.081

  • a property “target” (or whatever you choosed in the tracking object) type and value do not matter. It simply must exist.

I hope it helps you