[SOLVED] Hunter and Prey Help

so i put “attack” there and the error is here

    enemy = archer.findNearestEnemy()

saying that archer has no method findNearestEnemy

Try this instead.

enemy = archer.findNearest(hero.findEnemies())

Andrei

error saying archer has no method findNearest

here is code

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    enemy = archer.findNearest(hero.findEnemies())
    if enemy:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandarcher(friends)
            pass

Why do you past an array into a function that is supposed to work with an unit?

Andrei

so what do i put then? do i put archer?

No. look at this and you should figure it out.

Andrei

there is no error any more but now my hero wont do anything (he hasnt done anything at all since i started the lvl)

Can I see your new code?

Andrei

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    enemy = archer.findNearest(hero.findEnemies())
    if enemy:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandarcher(friend)
            pass

im starting to hate this lvl

Shouldn’t you first find the coin and then check if it exists?

Andrei

sorry? (20characters)

Why do you check if something exists and then try to find it?

Andrei

so i should flip the two?

no, you shoul put an if item: in front

Yes, that is correct.

Andrei

so i put item there and now there is an error (also #Mumboformayor)

Can I see the code that you have now?

Andrei

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if item:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    enemy = archer.findNearest(hero.findEnemies())
    if enemy:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandarcher(friend)
            pass

#Mumboformayor

hey, thanks! lets stay on topic though :smile: