Enemy tracking issue

Hey folks, I’m working on a mock up for a game, specifically on the enemy tracking system. I’m aiming for a Zelda-esque feel, where the player tracks to the nearest enemy while a certain button is held, but can roam free otherwise. I’ve gotten a “track to enemy” script (that I found on the forums) working such that when i press Shift it will track to the enemy, however the only way to clear that tracking is to have it track to a different enemy.

So, could anyone help me out with a way to clear the object being tracked to when Shift is not being pressed? I feel like it should be a simple affair (else: track_ob.setObject(none)?) but I only know the bare bones of python so I’m drawing a blank. I can post my .blend if that would help, but unfortunately I can’t remember who wrote the script. Thank you for your time!

-Elgree

one sec…

http://www.pasteall.org/39810/python

Sdgeoff is da man

Woah. Although this looks amazing, I tried to implement it for almost an hour and couldn’t get anything. It’s definitely over my head, and I’m using 2.49 if it makes a difference. Regardless, I already have tracking working, I just need a way to clear the tracking when Shift isn’t being pressed.

Thanks for the quick reply!

one sec, I’ll dig up a working copy

:slight_smile:

Attachments

Meep.blend (506 KB)

I really appreciate your help. However, like I said, I’m using 2.49, (every newer version has major GUI bugs for me) so that file didn’t work. I’m really just looking for a way to make my player object track to nothing after it tracks to an object.

So, here’s the file, and I need the tracking only to last as long as Shift is being held down, and once it’s let go, the object is free to turn on it’s own.

track to closest.blend (137 KB)

Here’s 2 scripts that track to the nearest object with property:enemy, useful for homing missles and whatever else you can come up with.

1.Give each enemy a property:enemy
2.Give the tracking object these logic bricks:
Always—>python:trackToClosest—>EditObject:TrackTo, named “trackTo”
--------------------------------------------------->Any Positive-Y motion, set to Local
3. Use what ever method you prefer for creating projectiles and damage. I’m currently using a missle with no collision and a ray that checks in front for prop:enemy. If the ray is positive, it ends the missle object and inflicts damage on the target.

Demo file:
http://christopherschons.com/temp/blender/trackToNearestEnemy.blend

  1. Create a list of all the enemies within range 100 of the player.

import GameLogic as g
cont = g.getCurrentController()
own = cont.getOwner()
scene = g.getCurrentScene()
objList = scene.getObjectList()

#create a list of all enemies within range 100
#change this number to best fit your game area
g.enemyList=
for i in range(0,len(objList)):
obj = objList[i]
if hasattr(obj,“enemy”):
if own.getDistanceTo(obj) <100:
g.enemyList.append((obj.name))
2. Create a list of distances to enemies, relative to the script owner, then sorts the list. enemyRanges[0] is the enemy with the smallest distance from the object.

import GameLogic as g
cont = g.getCurrentController()
own = cont.getOwner()
objList = g.getCurrentScene().getObjectList()

track = cont.getActuator(“trackTo”)
motion = cont.getActuator(“motion”)

#check to see if there are any enemies within range
if len(g.enemyList) > 0:

#create a list of the enemies within range and their distances
#each list item has 2 parts: (distance, OBname)
enemyRanges =
for i in range(0,len(g.enemyList)):
enemy = objList[g.enemyList[i]]
dist = own.getDistanceTo(enemy)
enemyRanges.append((dist,enemy.name))

#sort the list. the first object will be the one with the shortest distance
enemyRanges.sort()

#get the second item (OBname) of the first list item, then strips ‘OB’ from the string
nearestEnemy = enemyRanges[0][1][2:]

#set your object to track to the nearestEnemy
#the name returned is “OBenemy”, but the trackTo actuator just needs “enemy”
#the [2:] removes the “OB” from the object name for proper tracking
track.setObject(nearestEnemy)

#activate the actuators
g.addActiveActuator(track,1)
g.addActiveActuator(motion,1)

else:
#if there are no enemies, activate only the motion actuator
g.addActiveActuator(track,0)
g.addActiveActuator(motion,1)

here ya go

I had actually found that one on my own, and like I said, I already have the tracking script. I managed to fix it on my own using logic states, but thanks for your time.

Try states?

you can use something like this:

if shift.positive:
controller.activate(actuator)
else:
controller.deactivate(actuator)

You do not enen need python to deactivate a controller.