near.getHitOwner?

Is it possable to get the name of the object that is emitting a property that is picked up by a near sensor, for instance if near property “enemy” get the emitters OB name? I would even be happy if I could use a ray for this!!

import GameLogic
controller = GameLogic.getCurrentController()
s = controller.getSensor(“NearSensor”)

if s.isPositive():
ob = s.getHitObject()
print ob.name

I believe you can use .getHitObjectList() too, if there’s more than one. I could be wrong though.

thanks guys! another stupid question - how do you put that into a Track to actuator?:o

EDIT: and another one - Can you give added objects they’re own name? so it instead of enemy, you pick up enemy.005?

ah I know what you wanna do lol :wink: you wanna have a (track to the nearest target) script I assume. but the thing is, what if there is more than 1 target in your near sensor? you may have to use this fonction

 # get controller
controller = GameLogic.getCurrentController()
 
# get object the controller is attached to
owner = controller.getOwner()
 
# get object list
objList = GameLogic.getCurrentScene().getObjectList()
 
# get object named Target
target = objList["OBTarget"]
 
# get the distance between them
distance = owner.getDistanceTo(target)
 

and have some kind of equation that look for the one with the nearest ‘‘target’’

I want to had such a script but I’m not at this point of my game yet

Well assuming your objList is an array of all the objects that are read in…

There’s probably other ways to do this, but this comes to mind first


x1,y1,z1 = owner.getPosition()
#Note: [0.0. 0], 0.0 - initialize default distance, 0 - initialize default index for objList
distanceInit = [0.0, 0]
 
for i in range(len(objList)):
    x2,y2,z2 = objList[i].getPosition()
    distance = x1*x2+y1*y2+z1*z2
    #Note: can also do sqrt(x1*x2+y1*y2+z1*z2), but it'll just waste more processing power
    if distance < distanceInit[0]:
       distanceInit[0] = distance
       distanceInit[1] = i

Basically after this scrip tis run, distanceInit[1] will hold the index value for the objList that contains the closest distance to the owner player. So to get the object, just do

objList[distanceInit[1]]

to get the name of the object to put into the vale of the TrackTo actuator

ob = objList[distanceInit[1]].getName()

:evilgrin:Untested, so it may not work, lol. But that’s the general idea behind it.

Jason Lin

it doesnt work, I printed objList[distanceInit[1]].getName()
but it only show the name of the last object added in your scene.

You can also use .getDistanceTo() to cut out a few lines…


distance = own.getDistanceTo(objList[i])

That’s what I proposed but how could we say to pick the object that as the shortest distance from the owner , ?

Here’s a link to a script that finds the nearest object with prop:enemy:
http://blenderartists.org/forum/showthread.php?t=90813&highlight=track+nearest+enemy

Here’s a test file:
http://christopherschons.com/temp/blender/trackToNearestEnemy.blend

It doesn’t use the near sensor, since I always had speed issues with it. Instead, it makes a list of all enemies, then the homing/tracking objects check the list for the closest object.

The enemy list script:
-Creates a list of all enemies within a certain range of the player.
-Sorts the list, so the first item is the closest object.

The Homing/Tracking script:
-Checks the distance between projectile and enemies.
-Finds the closest one.
-Sets the tracking actuator to that object.

If you can be a bit more specific at what you are trying to accomplish, I can probably give you a better solution. Are you trying to track to a certain type of object, an enemy, the player, etc.?

wow! thats perfect Skullbunny thanks :slight_smile: dont worry you’ll get the credits even if I modify a bit the script :wink:

Nice one skullbunny!!!

Thanks! I’m planing on trying my hand at an RTS game, or this will even allow NPC’s to help you fight in my RPG…Sooo many possibilities!!

Sorry it took me so long to post, I just got my old version of the Sims to run on my computer two days ago and now I’m a Sims junkie!:o

Hay skullbunny

IS ther a way to add a section of script which will say"if within a distance of (10), get next nearest object". If so…this could make an easy to use simple AI system…

do you mean like: if youre AI character see a ennemy to close of him (as you say within a distance of 10) he then dont care about this one an attack the second closest?

Basicly yes, although i was thinking more about waypoints, so he will move to the closest point. The way it is now he will only move to one point and get stuck (because that point is still the closest).