0
$\begingroup$

How do I make an object stop tracking to objects in other layers, but still have it track to those objects when they are in the same layer as itself?

Also, I need to be able to spawn the objects into the layer. I tried moving them to another scene, but was then unable to spawn them.

$\endgroup$
  • $\begingroup$ Are you using the logic brick to do the track to? $\endgroup$ – David Jul 23 '16 at 23:22
  • $\begingroup$ Yes. I have two objects from one layer spawn into another layer sperately, and one of the objects tracks to the other. $\endgroup$ – Thomas Jul 25 '16 at 0:27
2
$\begingroup$

You have a little misunderstanding of trackTo, layers and dynamically added objects.

Layers

Layers are only used when rendering (e.g. it influences the lighting of objects). There are no layers regarding logic!

The logic knows about

  • active objects (residing in enabled Blender layers) and
  • inactive objects (residing in hidden Blender layers).

TrackTo

The trackTo Actuator will track whatever object you tell it to track. The object has to exist within the scene.

The TrackToActuator can track inactive objects (objects at hidden Blender layers .. see above). I guess this is that you do not loose your actuator configuration when toggling through the layers while working with Blender.

Remark: The TrackToActuator can even track the owning object. That makes not much sense but is possible.

Within the BGE the target object will be determined when loading the scene. For example: if the Object parameter contains "Cube" the BGE will look for an object with the name "Cube".

  • If there is none, the actuator will not refer to an object (and not track at all).
  • If there is just one object, the actuator will referred to it.
  • If there is more than one object with that name, there is no guaranty which one will be referred to.

AddObject

AddOject (regardless if using Python or the actuator) adds new objects with the same name after the scene was loaded. This also means the trackTo actuator is already configured. You can add as many objects as you like, the trackTo actuator will not care (It will care when you remove the object it tracks).

Solution

When you dynamically add an object and you want to track it, you need to dynamically set a specific target object at the trackTo actuator (via Python).

You need to know

  • the trackTo Actuator
  • the object to track

The important question is

WHEN to track WHAT object.

Unfortunately your question does not include the When nor the What. At least it is not unique enough. Therefore I can't give specific advice.

I hope it helps a bit to understand the BGE's logic

Update #1

Thanks. So would a better way to ask be, "how do i make one object track to another object only when it spanws?

Yes, it is better. Answers:

  • when adding a target object
  • the latest added target object

Here we need to find out how to implement the answers into the code:

When is a target object added?

Only the adding object (adder) knows about that.

Suggestion: let the adder send a notification message. Any object listening to that message will know when a new object was added.

What is the latest object added?

A message is not sufficient, as it can only carry strings, but we need a game object. We could send unique criteria as string and perform a search. But I suggest we do it different.

Solution A

We assume the tracker knows the adder. The tracker asks the adder what object was added that it can use it for tracking. [This is not the best design as the tracker gets knowledge about the internal structure of the adder. You are also restricted to a single adder. But it is a simple design and shows you how you can proceed.]

import bge

adder = bge.logic.getCurrentScene().objects["Adder"]
addActuator = adder.actuators["add"]
target = addActuator.objectLastCreated

controller = bge.logic.getCurrentController()
trackToActuator = controller.actuators["track"]
trackToActuator.object = target
controller.activate(trackToActuator)

Run this code (as Python controller in script mode) when the tracker receives the message from adder.

enter image description here

enter image description here

Requirements:

  • the adder is named "Adder"
  • the adder's add object actuator is called "add"
  • the tracker's track to actuator is called "track"

When you want other names, then change the code snippet accordingly.

Solution B

We assume there is always just one added object. So we do a search by name over all active objects (which automatically skips the inactive original).

import bge

controller = bge.logic.getCurrentController()
messageSensor = controller.sensors[0]
if messageSensor.positive:
    targetName = messageSensor.bodies[0]
    targets = [o for o in bge.logic.getCurrentScene().objects 
                if o.name == targetName]
    target = targets[-1] # last object in list

    controller = bge.logic.getCurrentController()
    trackToActuator = controller.actuators["track"]
    trackToActuator.object = target
    controller.activate(trackToActuator)

As a gimmick I let track the last found object. In my version of the BGE, this was the last added object (of that name). Do not rely on it. This can change anytime making the assumption incorrect.

The logic changed a bit.

enter image description here

enter image description here

Requirements:

  • the message contains a body with the name of the added object
  • the tracker's track to actuator is called "track"

Several adders (with different added objects) are possible.

I hope it helps

$\endgroup$

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.