[SOLVED] Help! Rational Defense

Code
def hideUnits(units):
    for i in range(len(units)):
        unit = units[i]
        hero.command(unit, "move", {'x': 34, 'y': 10 + i * 12})

# The peasants know the order in which to build the traps.
peasants = hero.findFriends()
buildOrder = peasants[0].buildOrder
separator = ","
# Split buildOrder with a comma (",")
# and save the result to the variable `types`:
types = buildOrder.split(separator)

# There are the same number of peasants as types.
for index in range(len(peasants)):
    peasant = peasants[index]
    x = 16
    y = 10 + index * 12
    # Get buildType by `index` from the array of types:
    buildType = types[peasant]
    # Command the peasant to buildXY the buildType at x and y:
    hero.command(peasant, "build", buildType,{x, y})

# Watch for enemies and move peasants when ogres attack.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hideUnits(peasants)
        break

# Fight the ogres:
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("throw"):
            hero.throw(enemy)
        else:
            hero.attack(enemy)
    else:
        hero.moveXY(60, 35)
Error

image

Help please.
Lydia

@Lydia_Song I think this is the issue with your code. You didn’t actually write the function correctly. It should look something like this:

buildType = types[index]
    # Command the peasant to buildXY the buildType at x and y:
    hero.command(peasant, "buildXY", buildType, x, y)

Hope this helps!
Grzmot

1 Like

Thanks so much!
Lydia

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