Python settime and random choice error

Hi, I’m new to python and attempting to adapt a script for my own use. I’m struggling with a 2 problems.

First, i am trying to set a tracking time but this seems to break my script and cause it to do nothing.

Second, i want the object to pick a target randomly from a list but it constantly selects a new target.

The code is below and i have attached a demo of what I’m doing.

Many thanks, Mike

import bge
import random


def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner
    
    near = cont.sensors['CLOSE']
    see  = cont.sensors['VISION']
    hear = cont.sensors['HEARING']
    walls = cont.sensors['WallSearch']
    act = cont.actuators['tracker']
    targetNEAR = near.hitObjectList
    targetSEE  = see.hitObjectList
    targetHEAR = hear.hitObjectList
    
    if see.positive and walls.positive:
        target = random.choice(targetSEE)
        act.object = target
    #   act.setTime(10)
        cont.activate(act)
        
    elif near.positive and walls.positive:
        target = random.choice(targetNEAR)
        act.object = target
    #   act.setTime(10)
        cont.activate(act)
        
    elif hear.positive:
        target = random.choice(targetHEAR)
        act.object = target
    #   act.setTime(100)
        cont.activate(act)
        
    else:
        cont.deactivate(act)  

main()

HELPME.blend (404 KB)

Do something like this:


    if not "target" in own: //if a target hasn't been acquired
        if see.positive: //when see is positive
            own["target"] = random.choice(targetSEE) //set target


    else: //if target has been acquired
        track.object = own["target"] //set the track object
        cont.activate(track) //track

Thanks for the reply but i don’t seem to be able to get this to work, would it be possible for you to show it in my current code as im not sure im even inserting it correctly.

Regards, Mike

act.setTime(10)

should be

act.time = 10

fixes that one particular issue…but theres quite a mess there. I think the reason its constantly switching targets is because

 
if see.positive and walls.positive:  
        target = random.choice(targetSEE)
        print(id(target))  ##I ADDED THIS HERE TO SEE IT CONSTANTLY SWITCHING TARGETS
        act.object = target
        cont.activate(act)


see and walls are both positive for a few frames causing it to keep reassigning target over and over. You should have it assign a target only if one doesnt exist, once a target exist it should not set it again. unless your wanting it too when other conditions are met. But theres really a mess there. You have a wall sensor named wallsearch yet another called walls and 2 more with walls in it. You have 2 near sensors and 5 radar sensors…yet your still using hitObjectList. You could use hitObjectList from 1 near sensor (and maybe 1 radar if you didnt want to do math for FOV)and sort and handle everything in the script. That stuff is such a mess I cant tell what most of it does. Ill try to make something simpler using a script when I get home. Maybe someone will beat me to it.

Thanks for the reply, most of the radars are to determine which direction to turn by sensing if there is a wall on which side. The sensor names are pretty all over the place and i do not know python well enough to script it all but i am working my way through it to do the best i can.

thanks, Mike

Can anyone else help me with this issue?

Thanks, Mike