Finding objects via logic bricks and passing them to actuators

To me this feels like a basic and crucial feature for making anything in BGE. But I still haven’t been able to find out how it’s done, and hopefully it can be done via logic bricks somehow.

There are a lot of cases where you need to reference objects in actuators. However, you’re only allowed to choose an object already in the scene before BGE starts. You can’t use other logics to define an object, then reference that as an object to act upon. Or can you?

Here’s a simple example: Let’s say that when I press the Space key, I spawn a cube in a random location, and can spawn an infinite number of cubes. Those cubes have a game property called is-a-cube. In the middle of the scene there is a sphere. When I press Enter, I want the sphere to attach itself to the cube closest to it. It must do so by scanning the area for objects with the is-a-cube property. So: I select the sphere, add a Keyboard sensor for the Enter key, link it to an And controller, and link the controller to a Parent actuator. Now comes the problem: The “Parent Object” field of the actuator asks me for an object that’s already in the scene. I must tell it “the object named Cube.001”, not “the nearest object which has the game property is-a-cube” like I’d need to.

Do game logics allow for a way to get around this limitation? Can a set of logic bricks be used to detect an object, put it in a game property, then use that property in the Object field of an actuator? Or can actuators only be given the name of an object in the scene, and only scripts can be used if you need to detect an object dynamically?


import bge
cont = bge.logic.getCurrentController()
own = cont.owner


scene = bge.logic.getCurrentScene()
objects = scene.objects
distance=None
object=None
for ob in objects:
    if ob==own:
        continue
    if not own['property'] in ob:
        continue
    dist = own.getDistanceTo(ob)
    
    if distance==None:
        distance = dist
        object = ob
    if dist<distance:
        
        object = ob
        distance=dist
if not object==None:
    own.alignAxisToVect(own.getVectTo(object)[1],1,1)
    

in object that is looking for other objects

property = “X”

and in item to be tracked

Property-> X

Note rather then looking at all objects in scene, a collision sensor could be used instead, to build a list, and then “target” the closest. (this will be better once you have 100’s of objects)

Attachments

TrackToClosest.blend (435 KB)

Ouch… so no way without a script? I was afraid of that :frowning: Would really like to see BGE add a workaround via logic bricks for something this basic. Till then, I appreciate the script too and might try it later.

You can learn python :smiley:

I am 90% squirrels and I am learning :smiley:

I recommend you learning python:yes: not that hard to learn something basic, I learned python in two weeks and the basic API in 1 week:rolleyes: how? I suggested me, good luck and sorry english :smiley:

With pynodes, eventually we can make a repository of “python nodes” just like this one, but they have a input and output stream.

Like “Target” can feed another logic brick besides “track” like "if target is less the X distance apply force “Whatever”