Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Hunters prey Level, archers don´t defend reindeers - Help!


Hello coders! I need help:
This part of my code does not work:

def commandSoldier(soldier):
    # Soldaten sollen Feinde angreifen.
    for soldier in self.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy:
            self.command(soldier, "attack", enemy)
def commandArcher(archer):
    # Soldaten sollen Feinde angreifen.
    for archer in self.findFriends():
        enemy = archer.findNearestEnemy()
        if enemy:
            if distance<25:
                #self.command(archer, "move",{"x":20,"y":46})
                #self.command(archer, "attack", enemy)

    friends = self.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # Dieser Freund wird zu den veränderlichen Soldaten in commandSoldier zugeordnet.
        if friend.type == "archer":

The archers don´t wait to defend the reindeers, they run to the right and never come back. Who can help?
Sometimes I get the error message I can not command the Reindeers??


You have a mess of loops.

Your functions don’t need loops in them as you are already looping through every unit you command. If it is a soldier, you command soldier, but in the command soldier function you try to command everything on your team.

Delete the loops in the functions.