the code trouble

 
import bge
import mathutils
class Cycle:
def __init__(self, iterable):
self.iterable = iterable
self.index = 0
 
def __call__(self):
self.index += 1
 
if self.index >= len(self.iterable):
self.index = 0
 
return self.item
 
@property 
def item(self):
return self.iterable[self.index]
 
def patrol(cont):
own = cont.owner
scene = bge.logic.getCurrentScene()
try:
cycle = own['cycle']
 
except KeyError:
cows = [o for o in scene.objects if own.get("key", "cow") in o]
nodes = sorted(cows, key=lambda o:own.getDistanceTo(o))[:2]
cycle = own['cycle'] = Cycle(nodes)
 
threshold = own.get("threshold", 0.5)
turn_speed = own.get("turn_speed", 0.5)
forward_speed = own.get("speed", 1.0)
 
current_node = cycle.item
 
direction = current_node.worldPosition - own.worldPosition
distance = direction.magnitude
 
world_z = mathutils.Vector((0, 0, 1))
alignment = direction.cross(world_z)
 
own.alignAxisToVect(alignment, 0, turn_speed)
own.localLinearVelocity = [0, forward_speed, 0]
 
if distance <= threshold:
cycle() 
 
 

How do i make this code work more than ounce while in the blender game engine?

Please add the indentations. This hurts my eyes ;).

Please have a thought on this:

Is cycle a function?

Agoose77 wrote this code ask him.

 
import bge
import mathutils
class Cycle:
    def __init__(self, iterable):
        self.iterable = iterable
        self.index = 0
        
    def __call__(self):
        self.index += 1
        
        if self.index >= len(self.iterable):
            self.index = 0
            
        return self.item
    
    @property 
    def item(self):
       return self.iterable[self.index]
    
def patrol(cont):
 own = cont.owner
 scene = bge.logic.getCurrentScene()
 try:
  cycle = own['cycle']
  
 except KeyError:
  cows = [o for o in scene.objects if own.get("key", "cow") in o]
  nodes = sorted(cows, key=lambda o:own.getDistanceTo(o))[:2]
  cycle = own['cycle'] = Cycle(nodes)
 
 threshold = own.get("threshold", 0.5)
 turn_speed = own.get("turn_speed", 0.5)
 forward_speed = own.get("speed", 1.0)
 
 current_node = cycle.item
 
 direction = current_node.worldPosition - own.worldPosition
 distance = direction.magnitude
 
 world_z = mathutils.Vector((0, 0, 1))
 alignment = direction.cross(world_z)
 
 own.alignAxisToVect(alignment, 0, turn_speed)
 own.localLinearVelocity = [0, forward_speed, 0]
 
 if distance <= threshold:
  cycle()
 
 

What exactly is the problem? What happens when you try to run it more than once?

Also, it looks like Cycle objects can act as a function because they’re callable; the call method was defined in the Cycle class.

The enemy would not go near the closet cube with the property cow when i controlled that cube with a keyboard sensor and also go back to the other cube with the property cow.It is supposed to go backwards and forwards from two cubes that are the nearest that have the property cow out of ten cubes with the property cow.