Mixed unit tactics pyhton

Unhandeled error: Type error: cannot read property ‘isVector’ of undefined.

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
defendPoints = [{"x": 39, "y": 59},{"x": 39 ,"y": 54},{"x": 40 ,"y": 50}, {"x": 39 ,"y": 20}, {"x": 40 ,"y": 16}, {"x": 40 ,"y": 12}]

summonTypes = ['soldier', 'archer']

def summonTroops():
    type = summonTypes[len(hero.built) % len(summonTypes)]
    while hero.gold >= hero.costOf(type):
        hero.summon(type)

def commandTroops():
    friends = hero.findFriends()
    for friend in friends:  
        if friend.type != "palisade":
            for i in range(len(friends)):
                # Use % to wrap around defendPoints based on friendIndex
                # Command your minion to defend the defendPoint
                posi = defendPoints[i] % len(defendPoints)
                hero.command(friend, 'defend', defendPoints[posi])

def collectGold():
    item = hero.findNearestItem()
    hero.moveXY(item.pos.x, item.pos.y)
    
while hero.time < 61:
    collectGold()
    summonTroops()
    commandTroops()

This function has a few problems.
Firstly, why are you looping through your friends array twice? You do for friend in friends, then for i in range(len(friends)). You only want the for i in range(len(friends)). The first for loop is unnecessary.
Secondly, posi is incorrectly defined (this is actually very difficult, so I understand you having difficulty with it). At the moment, you are doing defendPoints[i] % len(defendPoints). Remember that defendPoints[i] is actually a coordinate, like {“x”: 44, “y”: 55}. You can’t use the % operator on it. Instead, you want to use i by itself, and divide that by len(defendPoints).
For more on % (modulo), you can read the post I wrote earlier, also about modulo, albeit in a different level:

Danny

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.