python Actuator problem

so i am making a tower defense game and need to find a way to make the towers shoot at enemy’s if they are in so many blender units of the tower.
i found that you cant change witch enemy you are shooting at with only logic bricks.
i am rather inexperienced in blender python this is my first script that is more than showmouse.

so i have my robots attaching your base and you can place towers.
on my tower i have a set up like this.

near sensor => python controller => track to actuator
radar sensor => and controller => add object actuator (shoot)

my shooting system works great if there is a robot in front of them they will shoot.
my aiming system works as long as i only have onerobot with the same name every time.
then i can just put in the robots name in track to and replace my python controller with a and controller.

this is the script that i have so far.

import GameLogic
def main():

cont = GameLogic.getCurrentController()
own = cont.owner

near = cont.sensors['near']
track = cont.actuators['track']

if near.positive:
    track.setObject('OBrobot')
    cont.activate(track)
else:
    cont.deactivate(track)

main()

this is the script i am trying to make

if near is true
get name of near’s hit object
set track to track near’s hit object
keep tracking that object until object is not in near’s range or has died
when that happens find a new target

my current problem is that i cant figure out how to change tracks target object.
i will also need to get near’s hit object and figure out if it is still in range even when there are multiple objects in range
this may be a easy to you but please help

changing trackTo target:
http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.KX_TrackToActuator-class.html#setObject
Be aware it does not need to be a string it can be a KX_GameObject. (e.g. one of hitObjectList)

list of colliding objects:
http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.KX_TouchSensor-class.html#hitObjectList
This will be a list of KX_GameObjects. So you have to pick one of them to be the target for the KX_TrackToActuator. It might help that you can calculate the distance between objects with getDistanceTo():
http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.KX_GameObject-class.html#getDistanceTo

that is what i am trying with this line of code but it dosent work
track.setObject(‘OBrobot’)

got it to work with just
track.setObject(‘robot’)

I see you solved your problem, but just thought I post anyway.

I’m trying to get something similar to this to work, but can’t. I see your code, and I am doing the same. I am trying to check a property on my enemy to see what node it is supposed to be going after (Node.001, Node.002, etc.), then set the trackTo actuator to track to that node. Here is what I have:

cont = GameLogic.getCurrentController()
own = cont.getOwner()
ownNode = GameLogic.getCurrentScene().getObjectList()["OBNode"]
trackTo = cont.actuators['trackTo']

if ownNode.Int == 9:
    ownNode.Int = 1
        
target = "Node.00" + str(ownNode.Int)
trackTo.setObject(target)
cont.activate(trackTo)

Now there are two problems that I know of. First, I am not able to set the node property integer in the way I am trying, which I know works for strings using .Text. But I can’t figure out how to do it like this. Also, even if I put “target = ‘Node.001’”, the enemy still does not track that node. So it’s not setting the object either. And I’ve also tried “trackTo.object = target” which did not work either. Any help would be appreciated.

Enormity i think you need something like this blend file i made a few weeks ago.The Red cube follows nodes you place around the level.

Attachments

Point and Click Control.blend (184 KB)

meatlover if you still need help PM me and i will make you a blend file for the tower system.

Wow, I’m pathetic. Your code did help me, but it’s not how you think. I had:

own = cont.owner()

I took off the ( )'s and it worked. rofl~ I seen you didn’t have those in yours.